How to replace a substring in a string with Rust

2 Answers

0 votes
fn main() {
    let s = "rust c c++ java";
    
    let result = s.replace("++", "--");
    
    println!("{}", result);
}




/*
run:

rust c c-- java

*/

 



answered Mar 5, 2023 by avibootz
0 votes
fn main() {
    let mut s = String::from("rust c c++ java");
    
    s = s.replace("++", "--");
    
    println!("{}", s);
}




/*
run:

rust c c-- java

*/

 



answered Mar 5, 2023 by avibootz
...