January 2019
Beginner to intermediate
554 pages
13h 31m
English
All strings in Rust are guaranteed to be UTF-8 by default, and indexing on string types in Rust does not work as you would use them in other languages. Let's try accessing the individual characters of our string:
// strings_indexing.rsfn main() { let hello = String::from("Hello"); let first_char = hello[0];}
On compiling this, we get the following error:

That's not a very helpful message. But it refers to something called the Index trait. The Index trait is implemented on collection types whose elements can be accessed by the indexing operator [] using index type as a usize value. Strings are valid UTF-8-encoded ...
Read now
Unlock full access