小编Tyn*_*ute的帖子

为什么 Rust 期望 () 应该布尔值?

我的 rust 代码应该返回一个布尔值,但由于某种原因, () 是预期的。这是怎么回事?

fn create_file(path: &Path) -> bool {
    // if file already exist
    if path.is_file(){
        false
    }
    let mut file = File::create(path);
    true
}
Run Code Online (Sandbox Code Playgroud)

错误:

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 `()`
Run Code Online (Sandbox Code Playgroud)

但是如果你加上“;” 在 false 之后,一切仍然有效。

rust

1
推荐指数
1
解决办法
96
查看次数

标签 统计

rust ×1