Char Type in Rust

Pascal Precht
InstructorPascal Precht

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 2 years ago

This lesson discusses the char type in Rust, which holds 32 bit unicode values.

Instructor: [00:00] A character type or a char is a single Unicode character as a 32-bit value. They're created using single quotes. Here we see a couple of examples. Char1 is a character, '1'. Char2 is a character, 'a'. We can use any character Unicode that we like. Even emojis work, as we can see here.

[00:22] Some characters have special meanings, so they need to be escaped using a . Here we see a \ escaping a , or a \ escaping a single quote. The same goes for a new line, a carriage return, and so on and so forth.

[00:38] To see that this is working, we can, for example, create a new command here and say println!("{:?}" , "some more text"). Then we can give it, for example, char4 and run the program. We'll see, it'll output a backslash plus some text.

J. Matthew
J. Matthew
~ 3 years ago

They're created using single quotes.

That's interesting. So this is clearly distinct from a string slice &str, which is created using double quotes. I see that they're separate types. Therefore 'a' != "a", which is a bit of a mind-bender.

Could you explain a little more about the difference between them? I recall that a string literal (as an example of a string slice) is a pointer to a hardcoded location within the binary. What's the deal with char?

Pascal Precht
Pascal Prechtinstructor
~ 3 years ago

So a char is always 4 bytes in size and therefore differs from the String or &str representation. From the docs:

let v = vec!['h', 'e', 'l', 'l', 'o'];

// five elements times four bytes for each element
assert_eq!(20, v.len() * std::mem::size_of::<char>());

let s = String::from("hello");

// five elements times one byte per element
assert_eq!(5, s.len() * std::mem::size_of::<u8>());

Notice that a list of chars take up much more memory than a String of the same content. I believe it's probably useful to use char over String when there's a need to run certain calculations/methods on the given character, as char's API is quite rich.