Is there a better way to propagate None into a value (not just return None), other that wrapping it into a function like this to allow use of ? operator (value itself is Option)?
struct A {
b: Option<B>
}
struct B {
c: Option<C>
}
struct C {
val: i32,
val_optional: Option<i32>
}
fn propagate<T>(f: impl FnOnce() -> Option<T>) -> Option<T> {
return f();
}
#[test]
fn foo() {
let abcv = Some(A {b: Some(B {c: Some(C {val: 42, val_optional: Some(42)})})});
let val = propagate(|| abcv?.b?.c?.val_optional);//why not allow just let val = abcv?.b?.c?.val_optional ?
assert_eq!(val, Some(42));
let ab = Some(A { b: Some(B { c: None }) });
let val = propagate(|| ab?.b?.c?.val_optional);//why not allow just let val = abcv?.b?.c?.val_optional ?
assert_eq!(val, None);
let abcv = Some(A {b: Some(B {c: Some(C {val: 42, val_optional: Some(42)})})});
let val = propagate(|| Some(abcv?.b?.c?.val));//why not allow just let val = Some(abcv?.b?.c?.val) ?
assert_eq!(val, Some(42));
let ab = Some(A { b: Some(B { c: None }) });
let val = propagate(|| Some(ab?.b?.c?.val));//why not allow just let val = Some(abcv?.b?.c?.val) ?
assert_eq!(val, None);
}