应为 `&str`,找到了 `&&str`

Expected `&str`, found `&&str`

这是我的代码:

let mut i = 0;
let mut deck: [[&str; 2]; 56] = [[""; 2]; 56]; //Default::default();
for suit in suits.iter() {
   for rank in ranks.iter() {
        deck[i] = [suit, rank];
        i += 1;
   }
}

这是错误:

error[E0308]: mismatched types
  --> src/main.rs:37:23
   |
37 |             deck[i] = [suit, rank];
   |             -------   ^^^^^^^^^^^^ expected `str`, found `&str`
   |             |
   |             expected due to the type of this binding
   |
   = note: expected array `[&str; 2]`
              found array `[&&str; 2]`

我不确定是什么问题。

问题是 iter()&T 上生成迭代器,在这种情况下,元素类型 (T) 是 &str,它是 &&str(双重引用)。但是 deck&str 的一个数组,而不是 &&str.

修复很简单:由于 &strCopy,您只需取消引用它:

deck[i] = [*suit, *rank];