()); ...">

Why is the size of `char` 4 bytes in Rust?

This code shows that it chartakes 4 bytes:

println!("char : {}", std::mem::size_of::<char>());
  • Why does it take 4 bytes ?.
  • Does the size depend on the platform or is it always 4 bytes?
  • If it is always 4 bytes, is this for something special?
  • Does the compiler provide a minimum size for size char?

At https://play.rust-lang.org/ I also get 4 bytes

+4
source share
3 answers

: a char Rust - , Scalar Unicode. , 💩 (aka Pile of Poo, aka U + 1F4A9), Rust char 128169 ( 0x1F4A9 ):

fn main() {
    let c: char = "💩".chars().next().unwrap();
    println!("💩 is {} ({})", c, c as u32);
}

.

, Rust char 4 , 4 - 2 , Scicar Unicode. , .


: Scalar , "", , , Unicode, char.

+8

char - . , . , .

- ; - , Unicode. .

+3

Char - , .

? UTF-8 Wikipedia.

128 (US-ASCII) . 1,920 . , . Unicode.

, Unicode, 4 .

-: http://www.eventhelix.com/realtimemantra/ByteAlignmentAndOrdering.htm

+2

All Articles