something with the "_" Placeholder in "match" which confuses me...
This works:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5c8ac044d6c720ae122f23517db714b8
but this is not working:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f1e7fc114e4f7fbfd3987267a8ead294
Compiler says:
match arms have incompatible types
I think the return value for the function is the reason for this behavior? And in my opinion the error message is also confusing, because the match arms have in both cases the same types?
But maybe I just overlook something?
play.rust-lang.org
Rust Playground
A browser interface to the Rust compiler to experiment with the language
so it's more or less not related to the _ Placeholder...but anyway I would like to know why this happens ... And is it possible to give back some kind of "empty" value when the return type of fn is i32 ?
have in both cases the same types no, they don't: match x { 1 => x+1, _ => (), } if x is 1, you return an i32, if it's something else, you return the unit type, (). these are different types
if you want to represent absence of value, use the Option type
match x { 1 => {println!("{}",x+1);}, _ => (), } this works because this: { println!("{}", x + 1); } returns () as well
Put never type or unreachable!() instead of unit type in _
Обсуждают сегодня