#Result Ok(()) not working?
1 messages · Page 1 of 1 (latest)
return Ok(());
legend
Hi @novel cloud , can you please explain why was there an error and why the solution worked?
Sure. In Rust everything is an expression, and because of that everything evaluates to some value. If you want to return a value at the end of a block, you do that by excluding the semicolon from that line.
error[E0308]: mismatched types
--> src/main.rs:3:5
|
1 | fn main() {
| - expected `()` because of default return type
2 | if 42 == 69 {
3 | "test 1"
| ^^^^^^^^ expected `()`, found `&str`
error[E0308]: mismatched types
--> src/main.rs:5:5
|
1 | fn main() {
| - expected `()` because of default return type
...
5 | "test 2"
| ^^^^^^^^ expected `()`, found `&str`
For more information about this error, try `rustc --explain E0308`.
?eval
if 42 == 69 {
"test 1"
} else {
"test 2"
}
"test 2"
But this only works if you are returning at the end of a block. If you want to return early (as DeGatchi wanted to) you use the return keyword
?play
fn test() -> u32 {
if true {
return 420;
}
42
}
Yup , got it. Thanks.
I love both of those numbers