#[derive(Debug)]
struct S{}
#[derive(Debug)]
struct E{}
fn test() -> Result<S, E> {
let data_1: Result<S, E> = Ok(S{});
let data_2: Result<S, E> = Err(E{});
let v: Vec<Result<S, E>> = vec![data_1, data_2];
for i in 1..2 {
for item in &v {
let val = item?; //error
println!("{:?}", val);
};
}
Ok(S{})
}
In the above code, I'd like to print the value of the item if the Result is Ok (otherwise return Err). But there is an error in (*item)? part due to moving a value behind shared reference:
[rustc E0507] [E] cannot move out of
*itemwhich is behind a shared reference move occurs because*itemhas typestd::result::Result<tests::S, tests::E>, which does not implement theCopytrait
I have tried cloning the data, but it doesn't solve the problem. Besides, it doesn't sound correct to clone.
What is the correct workaround/best practice for this?