我在使用try!
和?
宏时遇到了极大的困难,以至于我开始质疑现实的结构。我举起了straight from the rust-docs以下的例子,它仍然在我的脸上爆炸。
代码:
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]: the trait bound `(): std::ops::Try` is not satisfied
--> src/main.rs:6:23
|
6 | let mut file: File = File::open("foo.txt")?;
| ----------------------
| |
| the trait `std::ops::Try` is not implemented for `()`
| in this macro invocation
|
= note: required by `std::ops::Try::from_error`
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> src/main.rs:7:2
|
7 | file.write_all(b"Hello, world!")?;
| ---------------------------------
| |
| the trait `std::ops::Try` is not implemented for `()`
| in this macro invocation
|
= note: required by `std::ops::Try::from_error`
根据rustup
(1.19.0)
答案 0 :(得分:1)
目前预计这些示例将在包含Result
的函数中运行;如果单击示例右上角的“运行”,您将看到它扩展为:
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(())
}
}
这是因为返回Result
的函数(如File::create
和io::Write::write_all
)应该考虑到可能的错误(在文档示例中尤为重要)。
有一个RFC允许从Result
返回main()
已经合并的?
,main()
允许<div id="app">
<input type="text" v-model="model1" />
</div>
<div>
<div id="model2">ABCDEFG</div>
<input type="text" />
</div>
<script>
new Vue({
el: '#app',
data: {'model1': 'value'},
...
...
...
});
</script>
在primary-expression
}仍然有效。