小编Tom*_*ank的帖子

为什么这个借用仍然“活跃”?

这是我遇到的事情的简化示例:

trait Bla<'a> {
    fn create(a: &'a Foo) -> Self;
    fn consume(self) -> u8;
}

struct Foo;
impl Foo {
    fn take(&mut self, u: u8) {}
}

struct Bar {
    foo: Foo,
}

impl Bar {
    fn foobar<'a, 'b, B: Bla<'b>>(&'a mut self)
    where 'a: 'b {
        let u = {
            // immutable borrow occurs here
            // type annotation requires that `self.foo` is borrowed for `'b`
            let foo: &'b Foo = &self.foo;
            let b = B::create(foo);
            b.consume()
        };

        // error[E0502]: …
Run Code Online (Sandbox Code Playgroud)

rust

14
推荐指数
2
解决办法
898
查看次数

标签 统计

rust ×1