0

I am trying to compare two strings in Rust for a project I am doing in my free time in order to become more familiar with Rust, however I have run in to a small issue. Whenever I try to do string1 == string2 all I get is false. Here is the code that I am using:

use std::io;

fn main() {
    let mut username = String::new();
    let mut password = String::new();

    let username = "Miku";
    let password = "password";

    loop{
        let mut userAttempt = String::new();
        let mut passAttempt = String::new();

        println!("Enter username:\n> ");
        io::stdin().read_line(&mut userAttempt).expect("Failed to read line");

        println!("Enter password:\n> ");
        io::stdin().read_line(&mut passAttempt).expect("Failed to read line");

        println!("{}", userAttempt == username);

        if userAttempt == username{
            println!("Got here!");

            if passAttempt == password{
                println!("Logged in successfully.");
            }
        }
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

2

Check https://doc.rust-lang.org/stable/std/io/trait.BufRead.html#method.read_line - the return value from read_line contains the new line character.

You could do if userAttempt.trim() == username{ ... to remove the additional space characters

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187