rust - 这个“静态”的生命是从哪里来的?
问题描述
以下程序无法编译:
use std::any::Any;
trait Foo<'a> {
fn to_box_any(self: Box<Self>) -> Box<Any + 'a>;
}
fn test<'a>(v: Box<dyn Foo<'a> + 'a>) {
v.to_box_any();
}
fn main() {}
错误信息:
error[E0478]: lifetime bound not satisfied
--> src/main.rs:8:7
|
8 | v.to_box_any();
| ^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime 'a as defined on the function body at 7:1
--> src/main.rs:7:1
|
7 | fn test<'a>(v: Box<dyn Foo<'a> + 'a>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: but lifetime parameter must outlive the static lifetime
我以为我标记了尽可能多的显式生命周期,但我不知道static
生命周期要求来自哪里。
如果我Any
使用自定义特征进行更改,它会起作用,所以看起来像是Any
在创建需求?
解决方案
推荐阅读
- javascript - 在 lit-html/lit-element 中添加/编辑表单的最佳实践
- javascript - 模式在 Firebase 登录完成之前关闭并重新加载页面
- json - 反序列化 JSON 并将叶子数据放入 Rc 结构的最佳方法是什么?
- primeng - Primeng 表:在 ngOnInit 中向表中添加行
- java - 在 Java 类之间传递变量状态
- c++ - 项目包含路径中的“.cpp”文件将不会被项目看到
- windows - 如何通过 PowerShell 为 Windows 10 上的当前用户设置 Windows 开始菜单布局
- laravel - lumen 6:如何在 lumen 上重构响应分页的数据?
- c# - 如何在 Firebase 分析中记录用户名?
- python - 是否有用于纠正以下 TypeError 的 Python 函数?