Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

String replace limited times

  • replacen
fn main() {
    let text = String::from("Black cat, brown cat, many cats and a dog");
    println!("{text}");

    let new = text.replacen("cat", "mouse", 2);
    println!("{new}");
    println!("{text}");
}
Black cat, brown cat, many cats and a dog
Black mouse, brown mouse, many cats and a dog
Black cat, brown cat, many cats and a dog