Apologies I am having a hard time getting this to work in Rust playground. Consider the following:
struct Foo<T> {
bar: Vec<T>
}
Now in our product we have realized we do not need a vector since we will only ever have 1 of something at most. So we switch to option:
struct Foo<T> {
bar: Option<T>
}
In most places in our code this change is Completely fine. However we run into issues when T is Request<Body> (http::Request, http::Body). We had no issues before doing Foo.bar[0] to access data and change data on &mut Foo. Now if I do Foo.bar.unwrap() I get:
move occurs because `request` has type `http::Request<Body>`, which does not implement the Copy trait
Request<Body> does not support Copy or Clone.
Why is does Vec work for us but not Option? How do I make option work?
Index (of Vec) takes and produces references while unwrap uses value.
Lazy solution is to call .as_ref() before unwrap, but it it better to not write unwrap() and so handle the case where it is none, without the program panicking.