This might be a really dumb question, but I can't figure out a nice way to do it.
I have a function which accepts (let's say) three Option arguments, and I want to perform a validation step where I check that all are Some, then do something with those values if they are.
The naive approach below doesn't work because Option isn't copyable. I could provide a default value for each (maybe the empty string) and unwrap them initially, or do it one by one, but I don't know what the idiomatic way to do this for several at the same time is. Is this even a good idea in Rust?
fn f(a: Option<String>, b: Option<String>, c: Option<String>) -> Option<String> {
if a.or(b).or(c).is_none() {
println!("a, b, and c are all required");
return None;
}
let a1 = a.unwrap();
let b1 = b.unwrap();
let c1 = c.unwrap();
Some(format!("{}:{}:{}", a1, b1, c1))
}
A nice way to do it is to put them all in a 3-element tuple, then pattern-match them back out again:
if let (Some(a), Some(b), Some(c)) = (a, b, c) {
println!("You passed in {:?}, {:?}, and {:?}");
}
else {
println!("Need to pass in all three arguments");
}
Thanks, that looks nice. I tried adding that, but I can't get it to work (still complains about using a moved value when I try to unwrap). If I provide different names on the LHS, I don't seem to get the variables assigned at all.
Is there somewhere I can read up on how that pattern-matching in an assignment should work?
Thanks! The only reason I didn't try that was - I think - I'd assumed that any method on the option was causing the error, not just the or. (I was also wrong about what or does, so thanks for pointing that out too!)