Skip to content

[Rust Bug] IO输入字符串和String__new()不相等?

rust
use std::io;

fn main() {
    let mut s1 = String::new();   
    let mut s2 = String::from("ss123");   
    io::stdin()
    	.read_line(&mut s1)
    	.expect("Failed to read line");
    println!("{}", s1 == s2); 
    println!("{}", s1.trim() == s2); 
    // Input ss123
    // Output false
    // Output true
}

为什么呢??? 因为多了 \r 和 \n

输入前

rust1.png

输入值后

rust2.png