Std::io & .replace

Good evening.

I'm new to rust and i'm trying to make a program that helps me at work. Basically the idea is that i want to prompt some data to the program and the program will copy folders and rename them.

I got multiple issues, but one that i'm currently dealing with now is to replace some "placeholder" text in a string to a std::io input i give the program.

let mut foldername = String::new();
    
    io::stdin().read_line(&mut foldername);

   let arg7 = args6.replace("placeholder", &foldername);
    println!("Foldername = {}", foldername);

I'm able to input a folder name and i can println the folder name as well, but the args6.replace("FROM", "TO") is causing me some issues, probably because i have no idea what im doing.

I want to replace the "placeholder" text with the input name i give the folder. The .replace works when i manually give it a name like "test_folder", but i can't get it 2 work with the variable foldername.

Perhaps you meant:

    let arg7 = args6.replace("placeholder", foldername.trim());
    //                                                ^^^^^^^

So that you don't include the newline from the input.

If that's not it, can you describe the problem in more detail?

1 Like

What are you trying to change? You're printing foldername but that won't change at all (nor will arg6; only arg7 will be different)

This answer should fix problem, in addition I recommend anytime when you get input do not forget to clean.

For your situation like this:

fn main() {
    let mut a = String::new();
    io::stdin().read_line(&mut a).unwrap();
    let a = a.trim();
    let b = String::from("value");
    let c = b.replace("va", a);
    println!("{c}");
}
1 Like

God day,

Thank you for the explanation regarding cleaning the input! I can now edit my filepath and replace the "placeholder" text in the filepath with the input provided by the user!

1 Like