Rust书中的stdio示例使用不稳定的库功能' old_io' - 有哪些替代方案?

时间:2015-03-27 14:40:10

标签: rust

我从Rust book获取了以下Rust代码:

fn main() {
println!("Type something: ");

let input = std::old_io::stdin().read_line().ok().expect("Failed to read line!");

println!("Here's what you said: {}", input);
}

当我使用rustc hello.rs编译此示例时,我得到以下输出:

hello.rs:4:38: 4:49 error: use of unstable library feature 'old_io'
hello.rs:4     let input = std::old_io::stdin().read_line().ok().expect("Failed
to read line!");
                                                ^~~~~~~~~~~
hello.rs:4:49: 4:49 help: add #![feature(old_io)] to the crate attributes to ena
ble
hello.rs:4:17: 4:35 error: use of unstable library feature 'old_io'
hello.rs:4     let input = std::old_io::stdin().read_line().ok().expect("Failed
to read line!");
                           ^~~~~~~~~~~~~~~~~~
hello.rs:4:35: 4:35 help: add #![feature(old_io)] to the crate attributes to ena
ble
hello.rs:4:38: 4:49 warning: use of deprecated item, #[warn(deprecated)] on by d
efault
hello.rs:4     let input = std::old_io::stdin().read_line().ok().expect("Failed
to read line!");
                                                ^~~~~~~~~~~
hello.rs:4:17: 4:35 warning: use of deprecated item, #[warn(deprecated)] on by d
efault
hello.rs:4     let input = std::old_io::stdin().read_line().ok().expect("Failed
to read line!");
                           ^~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors

文档作者似乎没有编译问题(或者如果他/她这样做,他们没有指出任何补救方法)。使用std::io会产生类似的错误。我有什么选择?

1 个答案:

答案 0 :(得分:1)

这似乎是bug 23760。作为一种解决方法,做

fn main() {
    println!("Type something: ");

    let mut s = String::new();
    let count = std::io::stdin().read_line(&mut s).ok().expect("Failed to read line!");

    println!("Here's what you said: {} ({} bytes)", s, count);
}

不幸的是,Steve points out

  

这些是我们无法运行自动化测试的[例子] :(