Rust 带问号的std文档示例不';不编译
我在使用Rust 带问号的std文档示例不';不编译,rust,rust-macros,Rust,Rust Macros,我在使用时遇到了极大的困难,请尝试和?宏,我开始质疑现实的结构。我举了下面的例子,它仍然在我的脸上爆炸 代码: pub use std::fs::File; pub use std::io::prelude::*; fn main() { let mut file: File = File::open("foo.txt")?; file.write_all(b"Hello, world!")?; } 错误: pub use std::fs::File; pub use std
时遇到了极大的困难,请尝试代码>和?
宏,我开始质疑现实的结构。我举了下面的例子,它仍然在我的脸上爆炸
代码:
pub use std::fs::File;
pub use std::io::prelude::*;
fn main() {
let mut file: File = File::open("foo.txt")?;
file.write_all(b"Hello, world!")?;
}
错误:
pub use std::fs::File;
pub use std::io::prelude::*;
fn main() {
let mut file: File = File::open("foo.txt")?;
file.write_all(b"Hello, world!")?;
}
error[E0277]:未满足特性绑定“():std::ops::Try”
-->src/main.rs:6:23
|
6 |让mut file:file=file::open(“foo.txt”)?;
| ----------------------
| |
|特性“std::ops::Try”未为“(”实现`
|在这个宏调用中
|
=注意:`std::ops::Try::from_error'所需`
错误[E0277]:未满足特性绑定“():std::ops::Try”
-->src/main.rs:7:2
|
7 |文件。写下所有(b“你好,世界!”)?;
| ---------------------------------
| |
|特性“std::ops::Try”未为“(”实现`
|在这个宏调用中
|
=注意:`std::ops::Try::from_error'所需`
根据rustup
(1.19.0)的规定,我正在进行最新的Rust稳定版本的发布。这些示例目前预计将在返回结果的函数中运行;如果单击示例右上角的“运行”,将看到它扩展为:
fn main() {
use std::fs::File;
use std::io::prelude::*;
fn foo() -> std::io::Result<()> {
let mut file = File::create("foo.txt")?;
file.write_all(b"Hello, world!")?;
Ok(())
}
}
fn main(){
使用std::fs::File;
使用std::io::prelude::*;
fn foo()->std::io::Result{
让mut file=file::create(“foo.txt”)?;
文件。写下所有(b“你好,世界!”)?;
好(())
}
}
这是因为返回Result
s的函数(如File::create
和io::Write::Write_all
)在处理时应考虑到可能的错误(这在文档示例中尤其重要)
虽然main()
中的允许返回结果的命令仍然处于活动状态,但已合并的main()
中存在允许返回结果的命令