That doesn't solve the whole problem, though - as per the rule, "zero, one, infinity", you often want to return more than two values, and often none of them represents an error.
fn six_numbers() -> (u8, u8, u8, u8, u8, u8) {
(10, 20, 30, 40, 50, 60)
}
fn in_an_array() -> [u8; 6] {
[10, 20, 30, 40, 50, 60]
}
The return type must be sized, because otherwise how can we put that anywhere? But of course your answer can just be to put it on the heap inside a Box... fn heap_numbers() -> Box<[u8]> {
let mut slice = Box::new_uninit_slice(6);
slice[0].write(10);
slice[1].write(20);
slice[2].write(30);
slice[3].write(40);
slice[4].write(50);
slice[5].write(60);
unsafe { slice.assume_init() }
}From Rust's point of view
let (a, b) = function_which_returns_a_pair();
is not special compared to the idiomatic: while let Some(item) = stack.pop() { /* do stuff with items and the stack */ }
or if we hate tuples: let [a, b] = function_returns_an_array_of_two_items();In Rust "tuple unpacking" is really just a special case of pattern matching, which is a far more powerful concept and was actually taken from OCaml.