Home > OS >  Expected `&str`, found `&&str`
Expected `&str`, found `&&str`

Time:05-30

Here is my code:

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;
   }
}

This is the error:

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]`

I am unsure what the issue is.

CodePudding user response:

The problem is that iter() produces an iterator over &T, and in this case that the element type (T) is &str, it is &&str (double reference). But deck is an array (of arrays of) &str, not &&str.

The fix is simple: since &str is Copy, you just need to dereference it:

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