Reputation: 41
My rust code should return a boolean value, but for some reason, () is expected. What's the matter here?
fn create_file(path: &Path) -> bool {
// if file already exist
if path.is_file(){
false
}
let mut file = File::create(path);
true
}
Error:
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 `()`
but if you add ";" after false, then everything still works.
Upvotes: 1
Views: 569
Reputation: 42678
You are missing the return
or the else
. Using else
will make the if/else block to be the return expression
fn create_file(path: &Path) -> bool {
// if file already exist
if path.is_file(){
false
} else {
let mut file = File::create(path);
true
}
}
Upvotes: 5