0

Hey so I'm attempting to get a user input and reference that inside of an if statement but i cant quite figure out how to use the if statement correctly I assume I have everything right and even following documentation online it seems right from what I can observe.

Here's the code snippet I'm trying to work on

use std::{thread, time::Duration};

fn main(){
    let mut line = String::new();
    println!("Will you help the man? (Y/N)");
    let _b1 = std::io::stdin().read_line(&mut line).unwrap();
    
    print!("{}",line);

    let y = "Y";
    if line == y{
        println!("The man gives you a peice of gold and thanks you.");
        thread::sleep(Duration::from_secs(5))
    } 
    else{
        println!("You walk past the man and ignore him");
        thread::sleep(Duration::from_secs(5))
    }
 }

Xanthus
  • 55
  • 1
  • 9
  • Are you getting any errors? – Abdul Niyas P M May 18 '22 at 05:24
  • No errors; its just not detecting the if statement and running trough the else output @AbdulNiyasPM – Xanthus May 18 '22 at 05:25
  • `read_line` includes the newline character, use `.trim()` to remove it – kmdreko May 18 '22 at 05:26
  • BTW when using `print` to check the contents of `line`, you'll get more useful output with `println!("{:?}", line)`: it makes it easier to see whitespace characters at the ends of the string. In your case it would have printed `"Y\n"`, making the issue obvious. – Jmb May 18 '22 at 06:19

0 Answers0