- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
感谢您纠正我提问的方式。我进行了多次修改以使代码可编译。
use std::marker::PhantomData;
struct Brace {
x: i32,
}
impl Brace {
fn transform(&self, n: i32) -> Devil {
Devil {
hp: self.x + n,
weapon: None,
}
}
}
struct Bar<'a> {
tasty: &'a str,
}
struct Foo<'a, B>
where B: 'a + Into<Bar<'a>>
{
brace: Brace,
buz: Option<B>, // buz is of generic type B, and is able to be turned into bar.
phantom: PhantomData<&'a B>, // A marker that is used to resolve 'unused lifetime parameter a'
}
impl<'a, B: Into<Bar<'a>>> Foo<'a, B> {
fn transform_and_arm(self) {
// line B
let brace1: Brace = self.brace;
let mut devil: Devil = brace1.transform(12345); // line A
let buz = self.buz.unwrap();
// Before this line, it passes the compiler.
// Uncommenting the following line causes compiler to argue that the brace1 at line A doesn't live long enough. It says that borrowed value must be valid for the lifetime 'a as defined on the body at line B, but the borrowed value only lives until line C.
// devil = devil.arm(buz);
// Although adding the above line fails, making the weapon directly won't cause the compiler to complain.
// Uncommenting the following line passes compiler.
// let weapon = buz.into();
// The compiler stops the devil from arming itself before I even try to write the following line.
// devil.slay_the_world();
} // line C
}
struct Devil<'a> {
hp: i32,
weapon: Option<Bar<'a>>,
}
impl<'a> Devil<'a> {
fn arm<B: Into<Bar<'a>>>(mut self, biu: B) -> Devil<'a> {
self.weapon = Some(biu.into());
self
}
fn slay_the_world(self) {
unimplemented!()
}
}
transform_and_arm()
方法旨在通过移除 brace
和 buz
来使用 Foo
的实例.它调用 brace.transform()
使 brace
成为 Devil
。它通过用 buz.unwrap()
喂养魔鬼来强化魔鬼。
问题在于,调用 let weapon = buz.into();
是合法的,而调用 devil = devil.arm(buz);
会导致终身问题.
看来这个问题与生命周期有很大关系。如果没有'a for Devil,那么所有这些问题都会烟消云散。
最佳答案
编辑:我之前的回答没有很好地解释问题。这是另一种尝试。
你遇到这个问题是因为 transform
是有缺陷的。为了找出原因,让我们看看编译器为 transform
推断出的生命周期是多少? :
fn transform<'a>(&'a self, n: i32) -> Devil<'a>
我们可以看到编译器决定将返回的Devil
与对 self
的引用具有相同的生命周期传递给函数。
现在,让我们看看Devil
:
struct Devil<'a> {
hp: i32,
weapon: Option<Bar<'a>>,
}
我们可以看到 Devil
的生命周期参数与其 weapon
相关联.
回顾transform
, 然而...
fn transform<'a>(&'a self, n: i32) -> Devil<'a> {
Devil {
hp: self.x + n,
weapon: None,
}
}
...很明显 weapon
,这是 Devil
的唯一目的的生命周期参数,与对 self
的引用没有任何关系。 ,所以 &self
的生命周期和 Devil
应该彼此无关。
这会导致稍后在 Devil
时出现问题被分配了一个实际的武器,因为我们可能想要给 Devil
一个weapon
具有不同的生命周期,我们没有理由不能做到这一点。
此外,当前执行 transform
禁止返回 Devil
从超过对self
的引用, 而事实并非如此。
要解决此问题,请显式注释 transform
明确表示&self
和返回的 Devil
有不相关的生命周期:
fn transform<'a, 'b>(&'a self, n: i32) -> Devil<'b>
关于generics - 处理 `Into` 泛型时的 Rust 生命周期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44164826/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!