Rust experience sheet

Relevant Post: From Java to Rust

Answers to questions I’ve spent some time looking on the Internet to find an answer to, or which come to be relevant often.

Compare between String and &str

If we’re comparing 2 Strings and Rust shows the error expected 'String', found '&str' is because we forgot to add the .trim() part to buffer.

let mut buffer = String::new();
io::stdin().lock()
  .read_line(&mut buffer)
  .expect("Couldn't read user input");

if buffer.trim() == "yes" {
  // do action
}  

Parse from &str to i32

let index_str: &str = _matches.value_of("index")
    .unwrap();
let index: i32 = index_str.parse::<i32>()
    .unwrap();

Reference(s)

https://doc.rust-lang.org/std/
https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html
http://danielnill.com/rust_tip_compairing_strings