[英]Expected type `&Vec<u8>`, found `&Vec<&u8>`
我有這個小片斷,但它並沒有編譯和所有錯誤的事實題干combinations_n
回報&Vec<&u8>
代替&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 {
for (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| a + 2u8.pow(b as u32));
}
fn generate_flush_table() -> BTreeMap<u8,u8> {
let ft = BTreeMap::new();
let mut straight_counter = 1;
let mut other_counter = 323;
for 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();
for (key, value) in flush_table.iter() {
println!("{}: {}", key, value);
}
}
這是編譯器說的:
error: the trait bound `&u8: std::cmp::PartialEq<_>` is not satisfied [E0277]
if flush == [12, 3, 2, 1, 0] {
^~~~~~~~~~~~~~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
help: the following implementations were found:
help: <u8 as std::cmp::PartialEq>
note: required because of the requirements on the impl of `std::cmp::PartialEq<[_; 5]>` for `std::vec::Vec<&u8>`
error: mismatched types [E0308]
} else if is_straight(&flush) {
^~~~~~
help: run `rustc --explain E0308` to see a 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` to see a 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` to see a detailed explanation
note: expected type `&std::vec::Vec<u8>`
note: found type `&std::vec::Vec<&u8>`
我真的不知道如何的類型flush
真的可以&Vec<&u8>
因為combinations_n
返回CombinationsN
和我讀的文檔中
impl<I> Iterator for CombinationsN<I>
where I: Iterator,
I::Item: Clone
{
type Item = Vec<I::Item>
因此它實際上應該是Vec<u8>
。
作為一個專業的程序員,您應該學習產生一個最小,完整和可驗證的示例 。 這是您遇到的問題之一:
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();
}
失敗並顯示相關錯誤:
error: mismatched types [E0308]
let one_combination: () = RANKS.iter().combinations_n(5).next();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: run `rustc --explain E0308` to see a detailed explanation
note: expected type `()`
note: found type `std::option::Option<std::vec::Vec<&u8>>`
這表明, combinations_n
此特定調用確實會產生Vec<&u8>
,而不是Vec<u8>
。
那為什么呢?
來自CombinationsN
定義的這一行是關鍵:
type Item = Vec<I::Item>
CombinationsN
是迭代器適配器 ,因此I::Item
是其前面的迭代器的類型。 在我們的情況下,那是什么? slice::Iter
,它具有以下內容:
type Item = &'a T
因此,通過遍歷一個slice,您可以獲得對slice元素的引用 ,然后將引用本身傳遞給CombinationsN
,后者會克隆該引用並將其收集到Vec
。
一種解決方案是克隆迭代的元素:
RANKS.iter().cloned().combinations_n(5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.