#Result Ok(()) not working?

1 messages · Page 1 of 1 (latest)

austere gorge
#

I tired with the ; at the end but it also didn't work. Any idea?

novel cloud
#

return Ok(());

austere gorge
west harness
#

Hi @novel cloud , can you please explain why was there an error and why the solution worked?

novel cloud
heavy lotusBOT
#
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`.
novel cloud
#

?eval

if 42 == 69 {
    "test 1"
} else {
    "test 2"
}
heavy lotusBOT
#
"test 2"
novel cloud
#

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
}
heavy lotusBOT
west harness
#

Yup , got it. Thanks.

mossy ember