The Relation to String Slices
Explore Rust's approach to strings by understanding string slices, their UTF-8 encoding, and the role of characters and bytes. Learn why Rust uses UTF-8 to store strings efficiently and how this impacts string manipulation, including accessing character and byte data.
We'll cover the following...
This next bit isn’t strictly necessary to understand most programming in Rust, but I think it’s helpful.
There’s a data type we haven’t directly talked about yet, called a char. It represents a single character. This could be the letter A, or the @ sign, or the Hebrew letter Alef (א), or many other things. We’ll get back to that part. In Rust (and many other languages), a character literal is a character surrounded by single quotes, e.g., 'A'.
A string, logically, is a sequence of characters. You can think of "Hello" as ['H', 'e', 'l', 'l', 'o']. However, that’s not the way Rust actually represents a string. Instead, it does something totally different. ...