Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading