rust - 闭包不能返回引用吗?
问题描述
fn main() {
let a = String::from("foo");
let f = || &a;
fn_immut(f);
println!("{}", a);
}
fn fn_immut<F>(f: F)
where F: Fn() -> &String
{
println!("calling Fn closure from fn, {}", f());
}
这段代码无法编译,rustc
告诉我应该添加这样的代码'static
:
fn fn_immut<F>(f: F)
where F: Fn() -> &'static String
我试图这样做,但它仍然不起作用。并且rustc
还告诉我“这个函数的返回类型包含一个借来的值,但没有可以借用的值”。
我的问题是:在这段代码中,闭包已经a
在其范围内捕获了变量的引用,为什么 rustc 仍然告诉我“它没有借用的价值”?
解决方案
来自编译器的关键信息确实是缺少返回字符串的闭包的生命周期说明符。由于特征定义的签名Fn() -> &String
没有任何函数参数,因此编译器无法从中推断返回引用的生命周期。
error[E0106]: missing lifetime specifier
--> src/main.rs:10:16
|
10 | F: Fn() -> &String,
| ^ help: consider giving it a 'static lifetime: `&'static`
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
在此处添加'static
无助于解决问题,因为实际上返回的字符串不会有那个生命周期。需要在 中引入一个新的生命周期参数fn_immut
,从该参数可以转移到F
的约束。
fn fn_immut<'a, F>(f: F)
where
F: Fn() -> &'a String,
您还可以返回字符串切片 ( &str
) 而不是&String
. 完整代码:
fn main() {
let a = String::from("foo");
let f = || &*a;
fn_immut(f);
println!("{}", a);
}
fn fn_immut<'a, F>(f: F)
where
F: Fn() -> &'a str,
{
println!("calling Fn closure from fn, {}", f());
}
推荐阅读
- django - 字段“id”需要一个数字但得到“127.0.0.1”
- c - 如何使用 '%c' 和 scanf() 输入字符串
- android - Android 上的 ConfigFS 出现“功能未实现”错误
- javascript - 如何在电子 nodejs 中创建自定义字母字符映射
- python - 如果其中发生异常,实例化一个新的相同线程会导致堆栈溢出(python)吗?
- firebase - 访问 Firebase 存储时 App Engine 中的 502(错误网关)
- android - 如何使用导航组件在 Android 片段转换上收听转换完成?
- git - Git:如何从主仓库的子模块文件夹中跟踪文件?
- python - 更改表的数组中的变量
- mgcv - R mgcv:具有缺失值的 bam 自相关