如何解决为什么锈应该在何时沸腾?
我的rust代码应该返回一个布尔值,但是由于某些原因,应该使用()。 这是怎么回事?
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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。