Iterate in vector and change String?

Hi, How iterate in vector and change String ?
i try this but not work.

let mut s1 = String::from("Jesus Cristo [2016] - 087.mp4");
let s1 = s1.replace("-", ""); // this Work! Fine

let n_vector = vec!["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
for r in n_vector.iter() {  // it does not work :(
    let s1 = s1.replace(r, "");

The code compiles fine. What's your problem? If you mean that the changes to s1 are not visible after the loop, well, that's because you don't store them in s1 which is visible after the loop - you store them into another s1, which exists only for one iteration. Rust even hints on this problem:

warning: unused variable: `s1`
 --> src/
7 |     let s1 = s1.replace(r, "");
  |         ^^ help: if this is intentional, prefix it with an underscore: `_s1`
  = note: `#[warn(unused_variables)]` on by default
1 Like

I not remember to modify a variable outer of scope in rust. ??

I think you might want to try this change

for r in n_vector.iter() {
-   let s1 = s1.replace(r, "");
+   s1 = s1.replace(r, "");
1 Like

Yes is fine!. :hugs:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.