sách gpt4 ai đã đi

filter - 如何过滤 Vec 中的值并仍然返回 Vec?

In lại 作者:行者123 更新时间:2023-11-29 08:27:00 29 4
mua khóa gpt4 Nike

我有一个接受 &Vec 的函数(其中 Word 是本地定义的类型)并且应该返回 Vec (或 &Vec )。该函数的定义包括从参数中过滤值:

fn possibleWords(w1: &Word, words: &Vec) -> Vec {
words.into_iter().filter(|w| matches(w, w1)).collect::<>>()
}

但是,我收到以下错误消息:

error[E0277]: the trait bound `std::vec::Vec<[char; 10]>: std::iter::FromIterator<&[char; 10]>` is not satisfied

words.into_iter().filter(|w| matches(w, w1)).collect::<>>()
^^^^^^^ a collection of type `std::vec::Vec<[char; 10]>` cannot be built from an iterator over elements of type `&[char; 10]`

我不想返回切片,我真的想要一个 Vec .有没有办法不用手动构建 Vec从切片?

这是一个 MCVE:

type Word = [char; 10];

fn matches(w1: &Word, w2: &Word) -> bool {
ĐÚNG VẬY
}

fn possibleWords(w1: &Word, words: &Vec) -> Vec {
words.into_iter().filter(|w| matches(w, w1)).collect::<>>()
}


fn main() {
let w1 = ['D', 'E', 'C', 'O', 'U', 'V', 'E', 'R', 'T', 'E'];
let w2 = ['D', 'E', 'C', 'O', 'U', 'V', 'E', 'R', 'E', 'Z'];
let words = vec![w1, w2];
println!("{:?}", possibleWords(&w1, &words));
}

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com