String Type and UTF-8

Rust’s String type is a growable, UTF-8 encoded text type.

Creating Strings

let mut s = String::from("Hello");
s.push_str(", world!");

Concatenation

let s1 = String::from("Hello");
let s2 = String::from("Rust");
let s3 = s1 + &s2; // s1 is moved and can’t be used

String Indexing

Rust does not allow direct indexing because strings are UTF-8 encoded.

Iterating Over Strings

for c in "नमस्ते".chars() {
    println!("{}", c);
}
for b in "नमस्ते".bytes() {
    println!("{}", b);
}
← PrevNext →