在循环中使用read_line时的奇怪行为

时间:2017-07-21 08:37:09

标签: rust

我在Rust中的第一个程序应该以字符的形式从用户那里获取输入:C或F:

use std::io;

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

    let mut switch = true;
    while switch {
        println!("source unit? F or C?");
        io::stdin().read_line(&mut srcunit).expect(
            "failed to read src unit",
        );

        if srcunit.trim() == "F" || srcunit.trim() == "C" {
            println!("doing things right with {}", srcunit);
            switch = false;
        } else {
            println!("either F or C, not {}", srcunit);
        }
    }

    println!("you pressed {}", srcunit);
}

当我启动程序并按F或C时,它工作正常,所以我在这里跳过它。当我按下另一个角色时,奇怪的部分出现了。我希望我的程序再次询问F或C,直到我按下其中一个字符。当我这样做时,它应该离开while循环并告诉我我按下了什么。

source unit? F or C?
G
either F or C, not G //so far so good

source unit? F or C?
F
either F or C, not G //why is F not assigned to the srcunit variable? It's supposed to leave the loop now.
F                    //why does it print this line? I didn't press a key or have a println function/macro that does this

source unit? F or C?
V
either F or C, not G //it's still G, wtf
F                    //again, why are those two lines printed? Where does it store, that I pressed F previously?
V

1 个答案:

答案 0 :(得分:6)

来自documentation for read_line

  

读取所有字节,直到达到换行符(0xA字节),然后追加到提供的缓冲区。

(强调我的。)你需要在阅读下一行之前清除字符串,例如通过调用字符串上的clear()方法,否则答案将累积在变量中。

或者,您可以在循环中定义变量(但效率稍低,因为这样,String将无法重新使用已分配的存储空间用于下一个答案,必须将其解除分配并且再次重新分配。)

另见this question,最近询问。看起来这是一个常见的陷阱。