简体   繁体   English

不匹配的类型错误:预期的 `char`,找到参考

[英]Mismatched types error: expected `char`, found reference

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:不知道为什么&x不能被推断为&char和我提到的链接是:

  1. https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.all https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.all
  2. https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.contains https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.contains

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 . String::chars 返回一个char ,而不是&char ,并且 HashSet::contains 期望引用它包含的类型,所以&char However, closure |&x| dict.contains(x)然而,关闭|&x| dict.contains(x) |&x| dict.contains(x) expects a reference to some type. |&x| dict.contains(x)期望引用某种类型。
It is a little confusing, but &argument_name in function parameters essentially performs pattern matching on incoming parameter, dereferencing it.这有点令人困惑,但是 function 参数中的&argument_name本质上是对传入参数执行模式匹配,取消引用它。 It is the same as writing |(a, b)|和写成|(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 .所以真正的消息应该是Expected reference &_, found char

Actually I just went to the Rust playground and did what I suggested: First I removed the & from your |&x|实际上我只是去了 Rust 操场并按照我的建议做了:首先我从你的|&x|中删除了& . . Then the compiler complained that the contains(x) wasn't a reference, so I added a & there:然后编译器抱怨contains(x)不是引用,所以我在那里添加了&

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.这会编译并打印“false”,因为 X 不在 hash 集中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust 预期类型不匹配 (),找到结构`Enumerate - Rust mismatched types expected (), found struct `Enumerate 不匹配的类型需要 `i8`,发现 `()` - mismatched types expected `i8`, found `()` 错误[E0308]:预期类型不匹配 `()`,发现 `bool`。 如何消除此错误? - error[E0308]: mismatched types expected `()`, found `bool`. How to remove this error? 错误 [E0308]: 不匹配的类型 — 预期为 `&amp;str`,找到结构体 `std::string::String` - error[E0308]: mismatched types — expected `&str`, found struct `std::string::String` 错误[E0308]:类型不匹配 - 预期结构 `Test`,发现 `&amp;Test` - error[E0308]: mismatched types - expected struct `Test`, found `&Test` 错误:类型不匹配:尝试实现冒泡排序时,出现了预期的&#39;usize&#39;,并出现了&#39;&usize&#39; - error: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort “不匹配的类型预期单元类型`()`找到枚举`选项<String> `&quot; 尝试使用 Tauri 读取文件内容时出错 - "mismatched types expected unit type `()` found enum `Option<String>`" Error when trying to read the content of a file with Tauri 在全局静态变量中存储对外部 C 函数的静态函数引用:不匹配的类型:预期的 fn 指针,找到了 fn 项 - Store static function reference to extern C function in global static var: mismatched types: expected fn pointer, found fn item 类型不匹配:在分配字符串时预期&str找到了字符串 - Mismatched types: expected &str found String when assigning string if let语句中的类型不匹配:找到期望结果 - Mismatched types in if let statement: found Option expected Result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM