The .and_then() method is pretty useful, but how do I use it with a function that expects a reference? Using a closure works, but I wondered if there is a more elegant alternative.
fn add(x: &u32) -> Result<u32, u32> { Ok(*x + *x) } //stupid example
fn main() {
assert_eq!(Ok(2).and_then(|x| add(&x)), Ok(4)); // works, but closure
assert_eq!(Ok(2).as_ref().and_then(add), Ok(4)); // type error
}
.as_ref() also turns the Err variant of the result into a reference, which collides with the result type of add in the example.
The solution using the ok_ref method is nice. I did not know it's possible to implement traits on module foreign types. On a second read of the rust book, it's quiet obvious it is allowed :).
Does rustc actually optimise this kind of redirection (using closures) away?