misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/std/str.md "Suggest an edit")

#  Strings

There are two types of strings in Rust: ` String ` and ` &str ` .

A ` String ` is stored as a vector of bytes ( ` Vec<u8> ` ), but guaranteed to
always be a valid UTF-8 sequence. ` String ` is heap allocated, growable and
not null terminated.

` &str ` is a slice ( ` &[u8] ` ) that always points to a valid UTF-8
sequence, and can be used to view into a ` String ` , just like ` &[T] ` is a
view into ` Vec<T> ` .

    
    
    fn main() {
        // (all the type annotations are superfluous)
        // A reference to a string allocated in read only memory
        let pangram: &'static str = "the quick brown fox jumps over the lazy dog";
        println!("Pangram: {}", pangram);
    
        // Iterate over words in reverse, no new string is allocated
        println!("Words in reverse");
        for word in pangram.split_whitespace().rev() {
            println!("> {}", word);
        }
    
        // Copy chars into a vector, sort and remove duplicates
        let mut chars: Vec<char> = pangram.chars().collect();
        chars.sort();
        chars.dedup();
    
        // Create an empty and growable `String`
        let mut string = String::new();
        for c in chars {
            // Insert a char at the end of string
            string.push(c);
            // Insert a string at the end of string
            string.push_str(", ");
        }
    
        // The trimmed string is a slice to the original string, hence no new
        // allocation is performed
        let chars_to_trim: &[char] = &[' ', ','];
        let trimmed_str: &str = string.trim_matches(chars_to_trim);
        println!("Used characters: {}", trimmed_str);
    
        // Heap allocate a string
        let alice = String::from("I like dogs");
        // Allocate new memory and store the modified string there
        let bob: String = alice.replace("dog", "cat");
    
        println!("Alice says: {}", alice);
        println!("Bob says: {}", bob);
    }
    

More ` str ` / ` String ` methods can be found under the [ std::str
](https://doc.rust-lang.org/std/str/) and [ std::string ](https://doc.rust-
lang.org/std/string/) modules

##  Literals and escapes

There are multiple ways to write string literals with special characters in
them. All result in a similar ` &str ` so it's best to use the form that is
the most convenient to write. Similarly there are multiple ways to write byte
string literals, which all result in ` &[u8; N] ` .

Generally special characters are escaped with a backslash character: ` \ ` .
This way you can add any character to your string, even unprintable ones and
ones that