Start with the idea
Single quotes construct a char, while double quotes construct text. A char is one Unicode scalar value, not necessarily one byte. The crab emoji in the example fits in one char but occupies several UTF-8 bytes. &str is a borrowed view of UTF-8 text; its len() reports bytes. If you ask for chars().count(), you count scalar values instead. A user-perceived character can contain more than one scalar, such as a letter plus a combining mark, so even that count is not a universal measure of what a person sees.
Read the code
let crab: char = '🦀';
let word: &str = "Rust";
println!("{crab}: {} bytes", crab.len_utf8());
println!("{word}: {} bytes", word.len());The crab is one char but four UTF-8 bytes; Rust needs one byte per letter.
Practice starter
let text = "🦀";
println!("{}", text.len());
Work through it
Rust does not allow s[0] on a string because byte position zero is not a safe general-purpose character index. Instead, choose the operation that matches the question: bytes() when inspecting the encoding, chars() when processing scalars, and a grapheme-aware library when the interface needs visible clusters. This distinction prevents slicing through a multibyte UTF-8 sequence. Try printing the byte length and scalar count for plain ASCII and for the crab emoji. Predict both numbers before running, then compare them with the output and explain why they differ.
Consider the visible letter é. It can be stored as one scalar or as e followed by a combining accent; those forms can look the same while having different byte and scalar counts. That is why text handling should start from the user question, not from a convenient index. Byte length is useful for storage and protocols, scalar iteration for some parsing tasks, and grapheme clusters for many cursor and selection operations.
A Rust char is one Unicode scalar value, written with single quotes such as 'é'. A string slice &str is text encoded as UTF-8 bytes. A visible character can require more than one scalar value: a letter plus a combining accent or a family emoji may be multiple scalars even though a reader sees one symbol. This is why 'character count' needs a definition before you write the code.
ASCII letters use one UTF-8 byte, while many other scalars use several. text.len() returns byte length, not a count of visible symbols. text.chars().count() counts Unicode scalar values, which is still not always a count of what a person sees. Indexing a string by an arbitrary number could point into the middle of a multibyte encoding, so Rust does not provide text[0] as a universal character operation. Iterating over chars() or using a byte slice with checked boundaries makes the choice explicit.
Use a short ASCII word, one accented character, and one emoji. For each, predict len() and chars().count() before running. Then try to take a byte slice across a UTF-8 boundary and observe that an invalid boundary is rejected at runtime. A production UI may need a Unicode grapheme-segmentation library to count user-perceived characters. For learning, state whether your algorithm cares about bytes, scalar values, or visible graphemes; the right answer depends on the task.
Try it
Print a char and a borrowed string, then compare len() with chars().count() for ASCII and an emoji. Predict the byte and scalar counts before running.
What success looks like: Your prediction matches the output, and you can say which counting method answers which question without treating bytes as visible characters.
Check your understanding
What does len() report for &str?
Reveal the answer
- Visible symbols
- UTF-8 bytes
- Words
Use text.chars().count() when Unicode scalar values are the unit you need.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Characters & Text (courseId "basics", lessonId "chars-and-text") Link: https://crabrix.com/learn/basics/chars-and-text/ Goal: String indexing by an arbitrary byte is forbidden because UTF-8 boundaries matter. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/chars-and-text) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.