预期类型`bool`,找到类型`&bool`

JaM*_*JaM 2 boolean rust

我想bool从a中取一个Vec<bool>并在if语句中进行比较.如何解决以下错误?

  |
7 |             if cell {
  |                ^^^^ expected bool, found &bool
  |
  = note: expected type `bool`
             found type `&bool`
Run Code Online (Sandbox Code Playgroud)

if cell.clone() 适合我,但似乎有点hackisch.

She*_*ter 5

采取bool从Vec<bool>

就这样做:

let foo = vec![true];
if foo[0] { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

bool实现Copy,因此索引数组将复制值.

如果您在向量中引用了boolean,则需要取消引用它:

let foo = vec![true];
if let Some(val) = foo.last() {
    if *val { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)

要么

let foo = vec![true];
if let Some(&val) = foo.last() {
    if val { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)