I have a function that looks like this:
fn some_func() -> Option<(String, i32)>
When I tried to do this:
let (a, b) = some_func();
The compiler throws this error:
expected enum `Option`, found tuple
How can I receive the return values from the function?
You can call Some()
on some_func()
to access either the values (or in the event that there are no values, None()
).
The idiomatic way to do this in Rust is with a match
statement.
match some_func() {
Some((x, y)) => println!("{} {}", x, y),
None => panic!("no vals"),
}
Or if you wanna use assignment like in your example (but it won't handle None
):
let (a, b) = some_func().unwrap();
println!("{} {}", a, b);
Playground here.
And an updated playground with .expect()
instead of .unwrap()
here.
The larger point being that it is unlikely that you want to proceed in this block if (a, b)
is None
.