By the end of this lesson
Distinguish bytes, Unicode scalar values and what a reader sees as a character.
Before you start
- Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
- Complete lesson 4, or be comfortable explaining and running its examples.
Three words to know
- UTF-8
- A variable-width encoding of Unicode text.
- String
- Owned, growable UTF-8 text.
- Unicode scalar
- One value yielded by chars(), not necessarily one visible symbol.
1. Understand the idea
A String owns UTF-8 bytes. str is a view of valid UTF-8, commonly borrowed as &str. len() on a string counts bytes, while chars().count() counts Unicode scalar values. Neither is a general count of visible grapheme clusters: accents can be separate scalars. Rust does not allow text[0] to pretend a byte offset always identifies a whole character.
2. Save, run and observe
Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.
fn main() {
let name = String::from("café");
println!("bytes={}", name.len());
println!("scalars={}", name.chars().count());
let combined = "e\u{301}";
println!("combined_scalars={}", combined.chars().count());
}
What you should see · example output
bytes=5 scalars=4 combined_scalars=2
rustc --edition=2024 lesson.rs -o lesson
./lesson3. Explain the result
The final accented e is represented using e followed by a combining accent. It may look like a single symbol but chars yields two scalar values. If a field limit uses chars().count(), say so explicitly. Our final project uses a scalar limit and does not normalize text.
YOUR TURN
A small check before you continue
Would len() be a reliable user-visible name-length limit?
Show a suggested answer
No. It counts bytes. Even chars().count() is a scalar count, not a grapheme count; choose and document the intended unit.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
