gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:19:21 25 4
gpt4 key购买 nike

我有以下来自 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 会出现类似的错误。我在这里有哪些选择?

最佳答案

这好像是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 :

These are the [examples] we can't run automated tests for :(

关于rust - Rust 书中的 stdio 示例使用不稳定的库功能 'old_io' - 存在哪些替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29303347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com