ho bisogno di trovare un indice di un elemento in un vettore di stringhe, questo è quello che ho ottenuto finora:trovare indice di un elemento nel vettore
fn main() {
let test: Vec<String> = vec!["one".to_string(), "two".to_string(), "three".to_string()];
let index: i32 = test.iter().enumerate().find(|&r| r.1.to_string() == "two".to_string()).unwrap().0;
}
si tratta di produrre un errore
error: mismatched types:
expected `i32`,
found `usize`
(expected i32,
found usize) [E0308]
let index: i32 = test.iter().enumerate().find(|&r| r.1.to_string() == "two".to_string()).unwrap().0;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppongo che sia perché enumerate()
restituisce una tupla di <usize, _>
(correggimi se ho torto), ma come posso convertire usize
in i32
qui? Inoltre, se esiste un approccio migliore, sono aperto a suggerimenti.
FYI, minor nitpick - le tuple sono '(A, B)', non ''. – mdup