Code has error:
fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}
fn square(num: i32) -> i32 {
num * num;
}
Error:
⚠️ Compiling of exercises/functions/functions5.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
--> exercises/functions/functions5.rs:12:24
|
12 | fn square(num: i32) -> i32 {
| ------ ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
13 | num * num;
| - help: remove this semicolon to return this value
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
If we do:
fn square(num: i32) -> i32 {
num * num
}
That will works, because without ;
, Rust consider that line as implicity return value, if you have ;
, you need to add return
keyword:
fn square(num: i32) -> i32 {
return num * num;
}
标签:functions,square,i32,Implicitly,num,values,error,fn From: https://www.cnblogs.com/Answer1215/p/18029700