1. 5
    Char Type in Rust
    56s

Char Type in Rust

Pascal Precht
InstructorPascal Precht

Share this video with your friends

Send Tweet

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

J. Matthew
J. Matthew
~ a year 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
~ a year 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.