作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的rust代码应该返回一个 bool 值,但是由于某些原因,应该使用()。
怎么了
fn create_file(path: &Path) -> bool {
// if file already exist
if path.is_file(){
false
}
let mut file = File::create(path);
true
}
错误:
error[E0308]: mismatched types
--> src/main.rs:53:9
|
52 | / if path.is_file(){
53 | | false
| | ^^^^^ expected `()`, found `bool`
54 | | }
| | -- help: consider using a semicolon here
| |_____|
| expected this to be `()`
但是,如果您添加“;”错误之后,那么一切仍然有效。
最佳答案
您缺少return
或else
。使用else
将if/else块作为返回表达式
fn create_file(path: &Path) -> bool {
// if file already exist
if path.is_file(){
false
} else {
let mut file = File::create(path);
true
}
}
关于rust - 为什么 rust 应该期望()何时沸腾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63426862/
我是一名优秀的程序员,十分优秀!