gpt4 book ai didi

enums - "cannot infer an appropriate lifetime"使用闭包返回对枚举变量内容的引用

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

我有一个接受枚举引用的函数,我需要通过匹配枚举并读取其内容来解析它。枚举的一种变体(不在下面的简化的最小工作示例中)可能包含枚举本身的类型作为值,因此我可能需要递归调用相同的函数来解析它的值。

我想编写一个函数作为过滤器并返回一个 Option::Some 包含对枚举变体内容的引用,或者 None 如果该值必须被丢弃。

下面是一个最小的工作(不是真正的编译)示例:

enum Data<'a> {
Value(&'a String),
Null,
}

fn main() {
let s = String::new();
let d = Data::Value(&s);

let equal = |d: &Data| -> Option<&String> {
if let Data::Value(s) = d {
Some(s)
} else {
None
}
};

parse(&d, equal);
//parse(&d, equal_filter);
}

fn equal_filter<'a>(d: &'a Data) -> Option<&'a String> {
if let Data::Value(s) = d {
Some(s)
} else {
None
}
}

fn parse<'a, F>(data: &Data<'a>, filter: F)
where
F: Fn(&Data<'a>) -> Option<&'a String>,
{
filter(data);
}

Playground .

我尝试先使用闭包来编译代码,但在那种情况下我得到了错误:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:11:33
|
11 | if let Data::Value(s) = d {
| ^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 10:17...
--> src/main.rs:10:17
|
10 | let equal = |d: &Data| -> Option<&String> {
| _________________^
11 | | if let Data::Value(s) = d {
12 | | Some(s)
13 | | } else {
14 | | None
15 | | }
16 | | };
| |_____^
= note: ...so that the types are compatible:
expected &Data<'_>
found &Data<'_>
note: but, the lifetime must be valid for the expression at 18:5...
--> src/main.rs:18:5
|
18 | parse(&d, equal);
| ^^^^^
note: ...so that a type/lifetime parameter is in scope here
--> src/main.rs:18:5
|
18 | parse(&d, equal);
| ^^^^^

所以我尝试了一个函数,但是又遇到了另一个错误:

error[E0271]: type mismatch resolving `for<'r> <for<'a, 's> fn(&'a Data<'s>) -> std::option::Option<&'a std::string::String> {equal_filter} as std::ops::FnOnce<(&'r Data<'_>,)>>::Output == std::option::Option<&std::string::String>`
--> src/main.rs:19:5
|
19 | parse(&d, equal_filter);
| ^^^^^ expected bound lifetime parameter, found concrete lifetime
|
note: required by `parse`
--> src/main.rs:30:1
|
30 | / fn parse<'a, F>(data: &Data<'a>, filter: F)
31 | | where
32 | | F: Fn(&Data<'a>) -> Option<&'a String>,
33 | | {
34 | | filter(data);
35 | | }
| |_^

我更愿意使用闭包来解决问题,但我不知道如何使用函数来继续。

最佳答案

最终,这是由于 limitations in Rust's type inference 引起的.具体来说,如果将闭包立即传递给使用它的函数,编译器可以推断出参数和返回类型是什么。不幸的是,当它在使用前存储在变量中时,编译器不会执行相同级别的推理。

内联你的闭包并且它有效:

enum Data<'a> {
Value(&'a String),
Null,
}

fn main() {
let s = String::new();
let d = Data::Value(&s);

parse(&d, |d| match d {
Data::Value(s) => Some(s),
_ => None,
});
}

fn parse<'a, F>(data: &Data<'a>, filter: F)
where
F: Fn(&Data<'a>) -> Option<&'a String>,
{
filter(data);
}

但是,我鼓励您改为在枚举上创建方法并参与 idiomatic set of conversion functions :

enum Data<'a> {
Value(&'a String),
Null,
}

impl<'a> Data<'a> {
fn as_value(&self) -> Option<&'a str> {
match self {
Data::Value(s) => Some(s),
_ => None,
}
}
}

fn main() {
let s = String::new();
let d = Data::Value(&s);

parse(&d, Data::as_value);
}

fn parse<'a, F>(data: &Data<'a>, filter: F)
where
F: Fn(&Data<'a>) -> Option<&'a str>,
{
filter(data);
}

您的函数变体不起作用,因为您将相关的生命周期放在了错误的位置:

// Wrong
fn equal_filter<'a>(d: &'a Data) -> Option<&'a String>
// Right
fn equal_filter<'a>(d: &Data<'a>) -> Option<&'a String>

使用 #[deny(elided_lifetimes_in_paths)]#[deny(rust_2018_idioms)] 将引导您做到这一点:

error: hidden lifetime parameters in types are deprecated
--> src/main.rs:12:22
|
12 | let equal = |d: &Data| -> Option<&String> {
| ^^^^- help: indicate the anonymous lifetime: `<'_>`
|
error: hidden lifetime parameters in types are deprecated
--> src/main.rs:24:28
|
24 | fn equal_filter<'a>(d: &'a Data) -> Option<&'a String> {
| ^^^^- help: indicate the anonymous lifetime: `<'_>`

另见:

关于enums - "cannot infer an appropriate lifetime"使用闭包返回对枚举变量内容的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54677673/

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