rust - Expected type `&Vec<u8>`, found `&Vec<&u8>` -


i have small snippet, not compile , errors stem fact combinations_n returns &vec<&u8> instead of &vec<u8>.

extern crate itertools;  use std::io; use std::collections::btreemap; use std::iter::enumerate; use itertools::itertools;  const ranks: [u8; 13] = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0];  fn is_straight(hand: &vec<u8>) -> bool {     (i, h) in hand[1..].iter().enumerate() {         if h - hand[i] != 1 {             return false;         }     }     true }  fn hand_value(hand: &vec<u8>) -> u8 {     hand.iter().fold(0_u8, |a, &b| + 2u8.pow(b u32)); }  fn generate_flush_table() ->  btreemap<u8,u8> {     let ft = btreemap::new();     let mut straight_counter = 1;     let mut other_counter = 323;     flush in ranks.iter().combinations_n(5) {         if flush == [12, 3, 2, 1, 0] {             continue;         } else if is_straight(&flush) {             ft.insert(hand_value(&flush), straight_counter);             straight_counter += 1;         } else {             ft.insert(hand_value(&flush), other_counter);             other_counter += 1;         }     }     ft }   fn main() {     let flush_table: btreemap<u8,u8> = generate_flush_table();     (key, value) in flush_table.iter() {         println!("{}: {}", key, value);     } } 

here's compiler says:

error: trait bound `&u8: std::cmp::partialeq<_>` not satisfied [e0277]         if flush == [12, 3, 2, 1, 0] {            ^~~~~~~~~~~~~~~~~~~~~~~~~ help: run `rustc --explain e0277` see detailed explanation help: following implementations found: help:   <u8 std::cmp::partialeq> note: required because of requirements on impl of `std::cmp::partialeq<[_; 5]>` `std::vec::vec<&u8>` error: mismatched types [e0308]         } else if is_straight(&flush) {                               ^~~~~~ help: run `rustc --explain e0308` see detailed explanation note: expected type `&std::vec::vec<u8>` note:    found type `&std::vec::vec<&u8>` error: mismatched types [e0308]             ft.insert(hand_value(&flush), straight_counter);                                  ^~~~~~ help: run `rustc --explain e0308` see detailed explanation note: expected type `&std::vec::vec<u8>` note:    found type `&std::vec::vec<&u8>` error: mismatched types [e0308]             ft.insert(hand_value(&flush), other_counter);                                  ^~~~~~ help: run `rustc --explain e0308` see detailed explanation note: expected type `&std::vec::vec<u8>` note:    found type `&std::vec::vec<&u8>` 

i don't understand how type of flush &vec<&u8>, given combinations_n returns combinationsn , in documentation read

impl<i> iterator combinationsn<i>     i: iterator,           i::item: clone {     type item = vec<i::item> 

and should vec<u8>.

as professional programmer, should learn produce minimal, complete, , verifiable example. here 1 problem:

extern crate itertools; use itertools::itertools;  const ranks: [u8; 13] = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0];  fn main() {     let one_combination: () = ranks.iter().combinations_n(5).next(); } 

which fails related error:

error: mismatched types [e0308]     let one_combination: () = ranks.iter().combinations_n(5).next();                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ help: run `rustc --explain e0308` see detailed explanation note: expected type `()` note:    found type `std::option::option<std::vec::vec<&u8>>` 

this shows particular call of combinations_n produce vec<&u8>, not vec<u8>.

so why that?

this line definition of combinationsn key:

type item = vec<i::item> 

combinationsn iterator adaptor, i::item type of iterator precedes it. in our case, that? slice::iter, has this:

type item = &'a t 

so iterating on slice, references slice elements, , reference itself passed combinationsn, clones the reference , collects vec.

one solution clone iterated elements:

ranks.iter().cloned().combinations_n(5) 

Comments