Using String as a condition for a while loop

I have the following code :

`let mut input = String::new();
while input.to_lowercase() != "stop" {
    io::stdin().read_line(&mut input).unwrap();
}`

I keep getting the String does not implement the copy trait but I do not understand how to handle it.
I am learning rust so this is me trying to wrap my head around the language.

There must be something else, since this code runs without problems, other than the timeout on the playground that does not have stdin :slight_smile: Did you share all of your code?

Oddly enough your reply was exactly what I needed ^^
Yes later in the code I apparently borrowed the value and that is the source of the problem.
Thank you very much.

Furthermore if might bother.
I tried to test the function but when I type stop in the input It does not seem to trigger

that's probably because read_line() includes the trailing newline character.

1 Like

You can get rid of the trailing newline via the trim() method:

let mut input = String::new();
while input.trim().to_lowercase() != "stop" {
    io::stdin().read_line(&mut input).unwrap();
}

Good idea.
thank you everyone !

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.