UTF-8 and Chars
Unicode handling.
UTF-8 and Chars is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Rust Strings Are UTF-8
Every Rust String and &str is encoded as UTF-8. This means text can include any Unicode character, not just ASCII.
fn main() {
let s = String::from("caf\u{e9}");
println!("{}", s);
}What Is UTF-8?
UTF-8 encodes each Unicode character using one to four bytes. ASCII characters use a single byte; accented letters and emoji use more.
fn main() {
let ascii = "A"; // 1 byte
let accent = "\u{e9}"; // 2 bytes (e-acute)
println!("{} bytes vs {} bytes", ascii.len(), accent.len());
}Bytes vs Characters
Because of multi-byte encoding, len() returns bytes, while the number of characters may be smaller.
fn main() {
let s = "caf\u{e9}";
println!("bytes {}", s.len());
println!("chars {}", s.chars().count());
}The char Type Is Unicode
A Rust char is a 4-byte value that can hold any Unicode scalar value, far beyond a single byte.
fn main() {
let letter = 'Z';
let accented = '\u{f1}'; // n with tilde
println!("{} {}", letter, accented);
}Iterating Characters
Use chars() to walk through text by character. This correctly handles multi-byte characters.
fn main() {
let s = "a\u{e9}z";
for c in s.chars() {
println!("{}", c);
}
}Why You Cannot Index by Number
Rust does not allow s[0] on a string. Because characters vary in byte size, indexing by a single number would be ambiguous and unsafe.
fn main() {
let s = "hello";
// let c = s[0]; // error: cannot index a str
let first = s.chars().next().unwrap();
println!("{}", first);
}Getting the nth Character
Use the iterator method nth to fetch a character by position. It returns an Option.
fn main() {
let s = "abcdef";
let third = s.chars().nth(2);
println!("{:?}", third);
}Iterating Bytes
When you truly need raw bytes, use bytes() which yields u8 values.
fn main() {
let s = "AB";
for b in s.bytes() {
println!("{}", b);
}
}Slicing on Byte Boundaries
String slices use byte indices. Slicing must land on a valid character boundary, or the program panics.
fn main() {
let s = "hello";
let part = &s[0..3]; // valid ASCII boundary
println!("{}", part);
}char Methods
The char type has handy methods like is_alphabetic, is_numeric, and to_uppercase.
fn main() {
let c = 'k';
println!("alpha {} numeric {}", c.is_alphabetic(), c.is_numeric());
println!("upper {}", c.to_uppercase());
}Counting Specific Characters
Combine chars with filter to count characters matching a condition.
fn main() {
let s = "banana";
let a_count = s.chars().filter(|&c| c == 'a').count();
println!("a appears {} times", a_count);
}Quick Check
Recall why string length can surprise you.
Recap
UTF-8 and chars in Rust:
- Strings are UTF-8; characters use 1 to 4 bytes.
len()gives bytes;chars().count()gives characters.- You cannot index a string by number; use
chars()ornth. - Slices must fall on character boundaries.
fn main() {
let s = "sm\u{ee}le";
println!("bytes {} chars {}", s.len(), s.chars().count());
for c in s.chars() {
print!("{} ", c);
}
println!();
}Frequently asked questions
Is the “UTF-8 and Chars” lesson free?
Yes — the full text of “UTF-8 and Chars” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “UTF-8 and Chars”?
Unicode handling. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “UTF-8 and Chars” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String vs &str
- Slices
- String Methods
- UTF-8 and Chars