![](/img/trans.png)
[英]Tell the Rust compiler that the lifetime of a parameter is always identical to a struct's lifetime
[英]Compiler asking for lifetime in struct when lifetime is given
我正在尝试在“ SFML游戏开发”一书中编写示例,但是对于应该代表游戏世界的结构的生命周期存在问题。 错误如下:
extern crate sfml;
use self::sfml::window::*;
use self::sfml::graphics::*;
pub struct Game<'s> {
mWindow: RenderWindow,
mPlayer: &'s CircleShape,
}
错误信息:
error[E0106]: missing lifetime specifier
--> src/game.rs:8:18
|
8 | mPlayer: &'s CircleShape,
| ^^^^^^^^^^^ expected lifetime parameter
如果我给了我一生,为什么要一生?
如果我给了我一生,为什么要一生?
因为您没有在需要的时间里赋予它生命。 仔细查看错误消息。 它告诉您CircleShape
缺少生命周期,而不是对CircleShape
的引用 (尽管这也是必需的)。
查看CircleShape
的定义:
pub struct CircleShape<'s> { /* fields omitted */ }
生命周期已对其进行了参数化,因此您需要提供以下内容:
pub struct Game<'s> {
mWindow: RenderWindow,
mPlayer: &'s CircleShape<'s>,
}
我不能说这是否适合您的情况,但是应该编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.