Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

Rust String Methods

Strings come with a toolbox of methods: little built-in functions you call with a dot after the value, such as name.to_uppercase(). This page shows the ones you will use most. You do not need to memorise them; come back here whenever you need one.

Changing case and trimming

fn main() {
    let shout = "  hello rust  ";
    println!("[{}]", shout.trim());
    println!("[{}]", shout.trim().to_uppercase());
    println!("[{}]", "QUIET PLEASE".to_lowercase());
}
Output
[hello rust]
[HELLO RUST]
[quiet please]

The square brackets in the output are just there to show where the spaces are. trim removes spaces and newlines from both ends; trim_start and trim_end work on one end only.

Searching

fn main() {
    let sentence = "Rust makes systems programming friendly";
    println!("{}", sentence.contains("systems"));
    println!("{}", sentence.starts_with("Rust"));
    println!("{}", sentence.ends_with("!"));
    match sentence.find("programming") {
        Some(position) => println!("Found at byte {}", position),
        None => println!("Not found"),
    }
}
Output
true
true
false
Found at byte 19

find does not just answer yes or no; it tells you where the text starts. Because the text might not be there at all, it gives back an Option: Some(position) or None. We will meet Option properly later, and you will see it everywhere in Rust.

Replacing and splitting

replace gives you a new String with every match swapped. split cuts a string into pieces wherever a separator appears, and split_whitespace cuts at any spaces:

fn main() {
    let text = "I like tea. Tea is great.";
    println!("{}", text.replace("tea", "coffee"));

    let fruits = "apple,mango,banana";
    for fruit in fruits.split(',') {
        println!("- {}", fruit);
    }

    let words: Vec<&str> = "the quick brown fox".split_whitespace().collect();
    println!("{} words, the last is {}", words.len(), words[words.len() - 1]);
}
Output
I like coffee. Tea is great.
- apple
- mango
- banana
4 words, the last is fox

Notice that replace is case sensitive: "Tea" with a capital T stayed as it was. collect gathers the pieces into a Vec, a growable list, which has its own page soon.

From text to numbers and back

parse turns text into a number, and to_string turns almost anything into a String:

fn main() {
    let total: i32 = "40".parse::<i32>().unwrap() + "2".parse::<i32>().unwrap();
    let label = total.to_string() + " is the answer";
    println!("{}", label);
}
Output
42 is the answer

unwrap means "I am sure this worked, give me the value". It stops the program if parsing failed, so use it only when you really are sure.

Practice exercises

  1. Ask for a sentence and print it in capitals, and how many words it has.
  2. Replace every space in a sentence with an underscore.
  3. Split "10,20,30,40" at the commas, parse each piece, and print the total.