liveBook · Manning

Unicode

As explained in Learn Rust in a Month of Lunches

Unicode is a character encoding standard that assigns code points to characters across writing systems and symbols.

Unicode gives characters numbers. For example, A is 65 and 友 is 21451. Its range includes letters from many languages and symbols such as emoji. section 1.3

In Rust, a character has the type `char`. Familiar characters with small numbers can fit in a `u8`, which holds numbers from 0 through 255, but not every Unicode character can. section 1.3

fn main() {
    let str1 = "Hello!";
    println!("str1 is {} bytes and also {} characters.", str1.len(), str1.chars().count());
    let str2 = "안녕!";
    println!("str2 is {} bytes but only {} characters.", str2.len(), str2.chars().count());
}

Try the string-length example: `Hello!` takes 6 bytes for 6 characters, while `안녕!` takes 7 bytes for just 3. Use `.chars().count()` when you need the character count rather than the byte length. section 1.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines Unicode through numbered characters and illustrates byte counts, without detailing the encoding rules behind those counts.
Examples
Uses A, 友, an emoji and strings in English and Korean to contrast character numbers with byte counts.
What it assumes
Introduces character numbers and `u8` limits in plain terms rather than assuming knowledge of character encodings.
What you can do after
Evaluate/understand why a string’s byte length may differ from its character count.
How it connects
Links Unicode to `char`, `u8`, ASCII, type casting and string length.

Also explained in

Compare how 8 books explain Unicode →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.