- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始了一个非常小的程序来玩 Rust 中的解析器组合器,很快就遇到了一个我觉得很奇怪的错误:
trait Parser<T, E> {
fn parse<'a>(&self, input: &'a [u8]) -> Result<(&'a [u8], T), E>;
}
impl<F, T, E> Parser<T, E> for F
where
F: for<'a> Fn(&'a [u8]) -> Result<(&'a [u8], T), E>,
{
fn parse<'a>(&self, input: &'a [u8]) -> Result<(&'a [u8], T), E> {
(*self)(input)
}
}
// why can't I write:
// fn byte(b: u8) -> impl Parser<u8, ()> {
// this works, and the blanket Parser impl picks it up correctly
fn byte(b: u8) -> impl for<'a> Fn(&'a [u8]) -> Result<(&'a [u8], u8), ()> {
move |input: &[u8]| match input.first() {
Some(x) if *x == b => Ok((&input[1..], *x)),
_ => Err(()),
}
}
fn main() {
println!("{:?}", byte(b'c').parse(b"c123"));
}
byte
的注释掉的签名(返回 impl Parser<u8, ()>
)编译失败:
error[E0271]: type mismatch resolving `for<'a> <[closure@parser.rs:14:5: 19:6 b:_] as std::ops::FnOnce<(&'a [u8],)>>::Output == std::result::Result<(&'a [u8], u8), ()>`
--> parser.rs:12:19
|
12 | fn byte(b: u8) -> impl Parser<u8, ()> {
| ^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter 'a, found concrete lifetime
|
= note: required because of the requirements on the impl of `Parser<u8, ()>` for `[closure@parser.rs:14:5: 19:6 b:_]`
= note: the return type of a function must have a statically known size
我既不明白为什么需要绑定(bind)生命周期参数,也不明白具体的生命周期是多少。
在我看来,返回的闭包有一些无法形容的类型。这种类型实现了 for <'a> Fn(&'a [u8]) -> Result<(&'a [u8], u8), ()>
(并且编译器认识到这一点)。因此,它也应该由一揽子实现工具 Parser<u8, ()>
,或者我是这么想的。
最佳答案
The way I see it, the closure returned has some ineffable type. This type implements for <'a> Fn(&'a [u8]) -> Result<(&'a [u8], u8), ()> (and the compiler recognizes this).
仅当您指定它时。
考虑没有生命周期信息的函数签名:
fn byte(b: u8) -> impl Fn(&[u8]) -> Result<(&[u8], u8), ()>
其中,如果您写出省略的生命周期,则给出
fn byte<'a>(b: u8) -> impl Fn(&'a [u8]) -> Result<(&'a [u8], u8), ()>
这没有实现高阶 for<'a> Fn ...
- 它只实现了 Fn
一些固定的特征 'a
, 由调用者决定 byte
.
那是编译器提示的具体生命周期——它期望找到受 for<...>
约束的生命周期,它会找到已经描述过的生命周期。
关于rust - Blanket impl、HRTB 和 "impl"抽象返回类型 : "expected bound lifetime parameter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55340857/
我是Rust的初学者,无法编译以下代码。我知道这个问题可能并不新鲜,请尝试在各处搜索,但找不到以下问题的正确答案。 基本上,我试图从线程中调用方法,并且还使用不同的结构在线程之间发送和接收对象。 us
我的 lib.rs 中有以下结构 pub enum ConfigurationSource { StringContent(String), FileContent(PathBuf)
我有一个 trait Surface: 'static我想为 struct Obj 实现.特征需要是 'static因为我想存储 Surface 类型的对象在Vec> . 第一步我试过这个。 impl
我试图在可迭代序列中找到重复项。此外,我想知道到那时为止该序列中出现的元素。 我创建了一个 HashMap 并尝试从 take_while 使用的闭包中对其调用 insert。但是,由于与具体/绑定(
trait BT { fn get_a(&self) -> &A; } #[derive(Debug)] struct A { v: i32, } impl A { fn nb
在编写代码以适应 Rust 时,我偶然发现了一个编译器错误。我想了解为什么会收到错误以及如何处理: cannot infer an appropriate lifetime for lifetime
我试图了解 owned 的生命周期类,在用户定义的迭代器中使用时。考虑以下代码: var a = new owned C(); var b = new owned C(); a.i = 2; fora
我正在使用async-tungstenite来监听websocket,并使用async-std的StreamExt对结果流进行操作。 我想使用HashMap从websocket累积最新的股票行情指示器
我正在创建一个类似于映射的数据结构,其中每个值都提供了一种通过实现特征来检索其唯一键的方法。当临时创建关键字以例如在映射中查找值时,期望关键字引用相应值的一个或多个字段(或者仅引用堆栈数据等)。显然,
我正在创建一个类似于映射的数据结构,其中每个值都提供了一种通过实现特征来检索其唯一键的方法。当临时创建关键字以例如在映射中查找值时,期望关键字引用相应值的一个或多个字段(或者仅引用堆栈数据等)。显然,
我正在创建一个类似于映射的数据结构,其中每个值都提供了一种通过实现特征来检索其唯一键的方法。当临时创建关键字以例如在映射中查找值时,期望关键字引用相应值的一个或多个字段(或者仅引用堆栈数据等)。显然,
我想实现一个类似于调试 builders 的构建器由标准库定义。它们使用如下结构定义: struct DebugFoo { fmt: &'a mut std::fmt::Formatter }
我有点难过。我希望通过以下代码。 enum Source { String(&'self str), ReaderUtil(&'self ReaderUtil) } pub struc
我正在尝试获取 Windows 中耗时(以毫秒为单位)。我发现这样做的唯一方法是使用 FILETIME 结构获取自 1601 年以来的 100 纳秒数。但是 FILETIME 结构将值存储在两个变量中
对于 CakePHP 商店应用程序,我需要 365 天的 cookie 生命周期。该应用程序在使用 CentOS 7 和 Plesk 18.0.30、PHP 7.3 FPM 的托管 vServer 上
我最近遇到了一条我从未见过的借用检查器消息,我试图理解它。这是重现它的代码(简化的,现实生活中的例子更复杂)- playground : fn foo(v1: &mut Vec, v2: &mut V
据我所知,当我订阅铁路由器钩子(Hook)内的结果集时,如之前:或 waitOn:,它工作正常,但一旦另一条路线运行,它似乎就被拆除了。有谁知道这是否会发生? 假设是这样,这是否意味着使订阅在路由之间
我正在尝试将 str 转换为 Reader,但失败了。 下面的代码所做的是使用 with_str_reader 从 str 中提取 Reader use std::io::{with_str_read
当 a PayPal subscription is created 时,批准 URL 在响应中可用。 看起来像这样,包括一个 token : https://www.sandbox.paypal.c
在basic.life C++ 标准的一部分,可以找到以下内容(强调我的): The lifetime of an object o of type T ends when: if T is a cl
我是一名优秀的程序员,十分优秀!