Useful Functions

unwrap()

  • If we call unwrap it will make sure that if the result was an error, it will terminate our program, if it is a success it will yield the contents of it’s results.

  • We use it while taking input.

    #![allow(unused)]
    fn main() {
    io::stdin().read_line(&mut input).unwrap();
    }

trim()

  • This function removes all whitespaces from a string.

    #![allow(unused)]
    fn main() {
    let trimmed_string = input_string.trim();
    }

parse()

  • This function will parse the variable to different data type.

  • We’ll only have to provide the data type to the variable and the parse() will call the required parsing function.

  • The parse() returns a Result instead of parsed variable since it has a chance of failure. For Example, if we try to convert a string into floats which contains alphabets.

  • So, we use unwrap() along with parse, telling the compiler to stop the execution in case of failure.

    #![allow(unused)]
    fn main() {
    let weight: f32 = input.trim().parse().unwrap();
    }

or()

  • This function is used to handle the result and error variables.
  • If the result is ok, then it will return the unwrapped result, if error happens, it will return the error passed in the or function.
  • There is a question mark in the end, which is a shorthand for match block, if we don’t use question mark, we’ll have to still use the match block with or function.
#![allow(unused)]
fn main() {
// Traditional Method
match str::from_utf8(buf) {
    Ok(request) => {},
    Err(_) => return Err(ParseError::InvalidEncoding)
}

// Using or function
str::from_utf8(buf).or(Err(ParseError::InvalidEncoding))?;
}