Reputation: 35
I can't compare guess with word and it throws me an expected {&unknown}, found String. error
fn main() {
let mut guess = String::new();
let word: String = String::from("Hello, world");
for x in 1..10 {
io::stdin()
.read_line(&mut guess)
.except("failed to read line");
println!("{}", guess.eq(word)); //error is in this line
}
println!("Game is over");
}
Upvotes: 1
Views: 870
Reputation: 6071
You must borrow word
when you are comparing to it. So write guess.eq(&word)
. But this is equivalent to guess == word
, so I would recommend this instead.
Also when you are reading line the endline character '\n'
will also be added to guess
, so you should probably use guess.trim()
before compering to discard leading and trailing whitespaces.
Upvotes: 3