gpt4 book ai didi

rust - 减去 UniCase HashSet

转载 作者:行者123 更新时间:2023-11-29 08:08:40 27 4
gpt4 key购买 nike

我正在尝试运行以下代码:

extern crate unicase;

use unicase::UniCase;
use std::collections::HashSet;

fn main() {
let a = UniCase("a".to_owned());
let b = UniCase("b".to_owned());
let s1: HashSet<UniCase<String>> = [a].iter().cloned().collect();
let s2: HashSet<UniCase<String>> = [a, b].iter().cloned().collect();
let s3 = s2 - s1;
}

( Playground )

并得到这个错误:

error[E0369]: binary operation `-` cannot be applied to type `std::collections::HashSet<unicase::UniCase<std::string::String>>`

据我所知,HashSets 之间Sub 的要求是包含的类型实现Eq + Hash + Clone,它UniCase 似乎可以。任何指针?

最佳答案

the documentation可以看出, Sub 是为 references HashMaps 实现的:

impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S> 
where T: Eq + Hash + Clone,
S: BuildHasher + Default,

采用显式引用有效:

extern crate unicase;

use unicase::UniCase;
use std::collections::HashSet;

fn main() {
let a = UniCase("a".to_owned());
let b = UniCase("b".to_owned());
let s1: HashSet<_> = [a.clone()].iter().cloned().collect();
let s2: HashSet<_> = [a, b].iter().cloned().collect();
let s3 = &s2 - &s1;
println!("{:?}", s3);
}

无需指定 s1s2 的内部类型,但您必须克隆 a,因为它已移入数组。

关于rust - 减去 UniCase HashSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43503853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com