- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在可迭代序列中找到重复项。此外,我想知道到那时为止该序列中出现的元素。
我创建了一个 HashMap
并尝试从 take_while
使用的闭包中对其调用 insert
。但是,由于与具体/绑定(bind)生命周期相关的类型不匹配,到目前为止我还没有设法让它编译。</p>
这是我的代码的简化版本,它显示了相同的错误:
use std::collections::HashSet;
fn main() {
let mut seq = HashSet::new();
let mut insert = |k| seq.insert(k);
(1..10).cycle().take_while(insert);
}
这是我得到的错误:
error[E0631]: type mismatch in closure arguments
--> src/main.rs:6:21
|
5 | let mut insert = |k| seq.insert(k);
| ----------------- found signature of `fn(_) -> _`
6 | (1..10).cycle().take_while(insert);
| ^^^^^^^^^^ expected signature of `for<'r> fn(&'r {integer}) -> _`
error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:5:22: 5:39 seq:_] as std::ops::FnOnce<(&'r {integer},)>>::Output == bool`
--> src/main.rs:6:21
|
6 | (1..10).cycle().take_while(insert);
| ^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime
我需要如何更改代码才能使其正常工作?
最佳答案
这其实是变相的借用错误。
Iterator<Item = T>::take_while()
接受类型的关闭FnMut(&T) -> bool
- 也就是说,它通过引用将每个元素传递给闭包。这很自然,因为 take_while()
必须能够产生测试成功的元素,所以它不能按值传递它。
这意味着insert
参数类型被推断为 &_
,等等 HashSet
的通用参数也被推断为 &_
.但是,这意味着您正在尝试存储对 cycle()
产生的临时值的引用。指向生命周期更长的结构的迭代器。这是借用规则所不允许的。不幸的是,Rust 并没有准确地显示这种推理,因为出于某种原因它无法推断出数字类型是 i32
。而且它也无法为闭包推断出正确的生命周期参数。这就是你的错误所在。
相反,您的闭包应该在将参数存储到集合之前取消引用该参数。 This works :
use std::collections::HashSet;
fn main() {
let mut seq = HashSet::new();
let mut insert = |&k: &i32| seq.insert(k);
(1..10).cycle().take_while(insert);
}
我也必须添加完整的参数类型;正如我上面所说,我认为类型推断还不够强大,无法推导出它。
顺便说一句,如果您明确指定类型,您实际上可以获得借用检查器错误:
use std::collections::HashSet;
fn main() {
let mut seq = HashSet::new();
let mut insert = |k: &i32| seq.insert(k); // no dereference
(1..10).cycle().take_while(insert);
}
除了显式类型注释之外,上面的代码等同于您的原始示例,它会导致以下错误:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:5:43
|
5 | let mut insert = |k: &i32| seq.insert(k);
| ^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 5:22...
--> src/main.rs:5:22
|
5 | let mut insert = |k: &i32| seq.insert(k);
| ^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that expression is assignable (expected &i32, found &i32)
--> src/main.rs:5:43
|
5 | let mut insert = |k: &i32| seq.insert(k);
| ^
note: but, the lifetime must be valid for the block suffix following statement 1 at 5:5...
--> src/main.rs:5:5
|
5 | / let mut insert = |k: &i32| seq.insert(k);
6 | | (1..10).cycle().take_while(insert);
7 | | }
| |_^
note: ...so that variable is valid at time of its declaration
--> src/main.rs:5:9
|
5 | let mut insert = |k: &i32| seq.insert(k);
| ^^^^^^^^^^
关于closures - 从闭包填充集合时类型不匹配 "bound lifetime parameter"与 "concrete lifetime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807396/
我有一个非常简单的类,如下所示: abstract class Person { private $id; private $createdOn; // ... More pr
我需要在扩展AbstractClass的每个ConcreteClass中定义常量值。由于某种原因,对象最终会具有重复的字段,一组字段等于零,下一组字段具有正确的值。 某些接口(interface) p
在阅读 this article 中的工厂时,我遇到了这两个术语 AbstractFactory 和 ConcreteFactory。 在阅读 StackOverflow 时,我发现了一些很好的答案(
public class GenericClass { class MyClass { } public GenericClass(final T[] param) {
我在同一代码中遇到了另一个问题......我收到此错误: initialization method -initWithCharactersNoCopy:length:freeWhenDone: ca
我在研究 concolic testing 的概念时遇到了术语“具体和符号执行” . (那里提到的文章“CUTE:C 的混合单元测试引擎”在其摘要部分使用了该术语。) "The approach us
我在运行 Streaming API 时遇到了 tweepy 问题,但我的问题不仅仅与 tweepy 直接相关。 我遇到了多个异常,我认为我可以暂时“捕获/通过”,作为临时解决方案,直到找出问题所在。
我想将表单结果写入另一个数据库。 在 Concrete 5.6 中,您可以即时切换到另一个数据库。我无法找出它在 Concrete 5.7 中的工作原理。 5.6 方式 $db = Loader::d
我该怎么做才能防止编译器抛出以下警告 Missing concrete implementation of setter 'MyClass.field' and getter 'MyClass.fie
我们是 Glass mapper 的新手,想在我们的 Sitecore 项目中使用它。在查看教程时,我们注意到没有关于如何设置 Sitecore 允许的深度继承的深入示例。浏览网页时,我们注意到有人将
在自定义 Concrete5 代码 (5.7+) 中执行服务器端重定向的正确方法是什么? 最佳答案 我发现这是最好的方法: (new RedirectResponse('/URL-HERE'))->s
我正在开发一个建筑项目的库存控制系统。保管人负责添加新库存并将其分配给员工或从员工退还。这些项目(以及它们的属性)将有很大的不同。例如钢铁制品,服装,工厂/机械,工具等 我的问题是要使用Class/C
我有类Bar(可能还有许多其他类),它扩展了抽象类AbstractFoo。将 Bar 实例转换为 FooDTO 时,会检测具体类。 但是,当将 Bar 实例集合转换为 FooDTO 列表时,具体的类信
我有一个接口(interface)和一个定义如下的类 public interface IShape { } public class Square : IShape { } 我知道我可以在结构图中为
我正在this中学习工厂模式关联。在编写了一个普通的工厂之后,作者继续设计一个工厂,我们不必修改工厂的代码来添加新的具体实现。 (假设有一个 Product 接口(interface),工厂提供它的实
我有一个“提供者工厂”,它创建具体提供者的实现。要创建正确的实现,除其他参数外,它还需要 typeId。问题是为了将正确的 typeId 传递给工厂,我需要验证并在必要时更改它。为了做到这一点,除其他
在使用模型继承时,我试图找到 django 模型对象的实际类。 一些描述问题的代码: class Base(models.model): def basemethod(self):
我使用 vim 并尝试使用“Vundle”。它有助于像在 Ruby on Rails 中一样安装插件: Bundle 'scrooloose/nerdtree' Bundle 'scrooloose/
我对使用具体类和接口(interface)的影响有一些疑问。 说一些代码块(称之为chunkCode)使用具体类A。如果出现以下情况,我是否必须重新编译 chunkCode: 我向 A 添加了一些新的
所以我遇到了一个问题,我正在尝试实现父级的父级的具体版本,如下所示。 public abstract class Collection { ... } public abstract class
我是一名优秀的程序员,十分优秀!