the code snippet is:
use std::collections::HashSet;
fn main() {
let dna = String::from("ACCCX");
let dict: HashSet<char> = vec!['A', 'C', 'G', 'T'].into_iter().collect();
println!("{}", dna.chars().all(|&x| dict.contains(x)));
}
the error shown in compilation is:
error[E0308]: mismatched types
--> src/main.rs:6:37
|
6 | println!("{}", dna.chars().all(|&x| dict.contains(x)));
| ^--
| ||
| |expected due to this
| expected `char`, found reference
| help: did you mean `x`: `&char`
|
= note: expected type `char`
found reference `&_`
Not sure why &x
cannot be inferred as &char
and the links I referred to are:
Thanks!
The error message is a bit confusing, essentially it is backwards.
dna.chars().all(|&x| dict.contains(x)));
String::chars returns a char
, not &char
, and HashSet::contains expects a reference to the type it contains, so &char
. However, closure |&x| dict.contains(x)
|&x| dict.contains(x)
expects a reference to some type.
It is a little confusing, but &argument_name
in function parameters essentially performs pattern matching on incoming parameter, dereferencing it. It is the same as writing |(a, b)|
for a closure that takes a two element tuple and immediately destructures it into two variables.
So really the message should be Expected reference &_, found char
.
Actually I just went to the Rust playground and did what I suggested: First I removed the &
from your |&x|
. Then the compiler complained that the contains(x)
wasn't a reference, so I added a &
there:
use std::collections::HashSet;
fn main() {
let dna = String::from("ACCCX");
let dict: HashSet<char> = vec!['A', 'C', 'G', 'T'].into_iter().collect();
println!("{}", dna.chars().all(|x| dict.contains(&x)));
}
That compiles and prints "false" because X isn't in the hash set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.