String

The String type is a heap-allocated, mutable, sequence of UTF-8 encoded bytes. What this means is that the String type is stored on the heap, it can be modified, and it can store any character that is valid UTF-8 including emojis.

Creating a String

There are two ways to initialize a String type. The first is by using the String::from method which takes a string slice as an argument. The second is converting a string literal to a String type using the to_string method.

#![allow(unused)]
fn main() {
    // let greet = "Hello world!".to_string();
    let greet = String::from("Hello World!");
    println!("{}", greet);
}

If you don't know the content of the string yet, you can use the String::new method to create an empty String type.

#![allow(unused)]
fn main() {
    let mut empty_string = String::new();
}

Extending a String

There are two ways to insert a string into an existing String type. The first is using the push_str method which takes a string slice as an argument. The second is using the push method which takes a character as an argument.

#![allow(unused)]
fn main() {
    let mut greet_mutable = String::new();
    greet_mutable.push_str("Hello ");
    greet_mutable.push_str("World");
    greet_mutable.push('!');
    println!("{}", greet_mutable);
}