具有一个静态引用的异步函数编译:
pub async fn test_0(_a: &'static str) {
}
具有非静态和静态引用的异步函数编译:
pub async fn test_1<'a>(_a: &'a str, _b: &'static str) {
}
具有三个非静态引用的异步函数编译:
pub async fn test_2<'a, 'b, 'c>(_a: &'a str, _b: &'b str, _c: &'c str) {
}
一个接受两个非静态引用和一个静态引用并返回未来编译的函数:
pub fn test_3_desugared<'a, 'b>(_a: &'a str, _b: &'b str, _c: &'static str) -> impl std::future::Future<Output=()> {
std::future::ready(())
}
那么为什么一个带有两个非静态引用和一个静态引用的异步函数不能编译呢?
pub async fn test_3<'a, 'b>(_a: &'a str, _b: &'b str, _c: &'static str) {
}
有问题的编译错误:
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
--> src/lib.rs:11:74
|
11 | pub async fn test_3<'a, 'b>(_a: &'a str, _b: &'b str, _c: &'static str) {
| ^
|
note: hidden type `impl Future` captures lifetime smaller than the function body
--> src/lib.rs:11:74
|
11 | pub async fn test_3<'a, 'b>(_a: &'a str, _b: &'b str, _c: &'static str) {
|
我目前的锈版本:
$ rustc --version
rustc 1.56.1 (59eed8a2a 2021-11-01)
我在尝试将环摘要算法参数添加到已经有两个引用参数的函数时遇到了这个问题。解决方法非常简单,因为我可以将静态引用包装在一个结构中,将其传入,然后毫无问题地使用它。我只是好奇为什么会这样。