A Quick Rust tip (Comparing Strings)
I have been learning rust recently and so wanted to start tracking things that I’ve learned.
You can learn a lot more about strings in rust via this explainer but one thing I got hung up on was comparing strings.
There are two types of strings in rust, &str
and String
. It seems that rust is pretty good about deciding which type you want a "some string"
to be, but I was having trouble doing something like this:
I was worried that while guess
was a String
"Q"
was a &str
. Turns out the issue was with the way the read_line
formats the string when reading it. By default there is a newline character at the end of the string so you need to use trim()
.