I would like to take a bool
from a Vec
and compare it in an if statement. How do I solve the following error?
take a
bool
from aVec
Just do that:
let foo = vec![true];
if foo[0] { /* ... */ }
bool
implements Copy, so indexing the array will copy the value out.
If you had a reference to the boolean inside the vector, you will need to dereference it:
let foo = vec![true];
if let Some(val) = foo.last() {
if *val { /* ... */ }
}
Or
let foo = vec![true];
if let Some(&val) = foo.last() {
if val { /* ... */ }
}