谁借了一个变量? [英] Who borrowed a variable?

查看:94
本文介绍了谁借了一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和借阅检查器打架.我有两个类似的代码段,一个按我的预期工作,而另一个则没有.

I'm fighting with the borrow checker. I have two similar pieces of code, one working as I expect, and the other not.

可以正常工作的一个:

mod case1 {
    struct Foo {}

    struct Bar1 {
        x: Foo,
    }

    impl Bar1 {
        fn f<'a>(&'a mut self) -> &'a Foo {
            &self.x
        }
    }

    // only for example
    fn f1() {
        let mut bar = Bar1 { x: Foo {} };
        let y = bar.f(); // (1) 'bar' is borrowed by 'y'
        let z = bar.f();  // error (as expected) : cannot borrow `bar` as mutable more
                           // than once at a time [E0499]
    }

    fn f2() {
        let mut bar = Bar1 { x: Foo {} };
        bar.f(); // (2) 'bar' is not borrowed after the call
        let z = bar.f();  // ok (as expected)
    }
}

没有的那个:

mod case2 {
    struct Foo {}

    struct Bar2<'b> {
        x: &'b Foo,
    }

    impl<'b> Bar2<'b> {
        fn f(&'b mut self) -> &'b Foo {
            self.x
        }
    }

    fn f4() {
        let foo = Foo {};
        let mut bar2 = Bar2 { x: &foo };
        bar2.f(); // (3) 'bar2' is borrowed as mutable, but who borrowed it?
        let z = bar2.f(); // error: cannot borrow `bar2` as mutable more than once at a time [E0499]
    }
}

我希望像情况1一样,我可以调用Bar2::f两次而不会激怒编译器.

I hoped I could call Bar2::f twice without irritating the compiler, as in case 1.

问题在注释(3)中:谁借了bar2,而没有任何感情?

The question is in the comment (3): who borrowed bar2, whereas there is no affectation?

这是我的理解:

  1. 在情况1中,f2调用:生存期参数'a是接收的&Foo值之一,因此,在没有影响的情况下该生存期为空,并且不借用barBar1::f调用之后;

  1. In case 1, f2 call: the lifetime parameter 'a is the one of the receiving &Foo value, so this lifetime is empty when there is no affectation, and bar is not borrowed after the Bar1::f call;

在情况2中,bar2借用了foo(不可变),因此Bar2结构中的生存期参数'bfoo参考生存期,该生存期在身体.调用Bar2::f借用bar2在该生命周期内,即到f4结束.

In case 2, bar2 borrows foo (as immutable), so the lifetime parameter 'b in Bar2 struct is the foo reference lifetime, which ends at the end of f4 body. Calling Bar2::f borrows bar2 for that lifetime, namely to the end of f4.

但是问题仍然是:谁借了bar2?可能是Bar2::f吗?通话后Bar2::f如何拥有借入的所有权?我在这里想念什么?

But the question is still: who borrowed bar2? Could it be Bar2::f? How Bar2::f would hold the borrowed ownership after the call? What am I missing here?

我在x86_64-pc-windows-msvc上使用Rust 1.14.0-nightly(86affcdf6 2016-09-28)

I'm using Rust 1.14.0-nightly (86affcdf6 2016-09-28) on x86_64-pc-windows-msvc.

推荐答案

嗯...您基本上是自我借用的.

Ah... you basically self-borrowed yourself.

问题取决于以下事实:您具有与Foo的寿命和Bar的寿命相同的寿命('b).然后,编译器会忠实地合并这些生存期,最终您会遇到一种奇怪的情况,即突然本来应该在语句末尾结束的借用生存期在值超出范围后才结束.

The issue hinges on the fact that you have the same lifetime ('b) used for both the lifetime of Foo and the lifetime of Bar. The compiler then dutifully unifies those lifetimes, and you end up in a strange situation where suddenly the lifetime of the borrow which should have ended at the end of the statement instead ends after the value should have gone out of scope.

根据经验:始终self使用新的生存期.还有什么很奇怪的.

As a rule of thumb: always use a fresh lifetime for self. Anything else is weird.

有趣的是,这种模式实际上是有用的(尽管更可能是不可变的借位):它允许将锚定一个值到堆栈帧,从而防止在调用函数后发生任何移动,(有时)对于表示Rust不能很好建模的借阅很有用(例如,将指向该值的指针传递给FFI).

It's interesting to note that this pattern can actually be useful (though more likely with an immutable borrow): it allows anchoring a value to a stack frame, preventing any move after the call to the function, which is (sometimes) useful to represent a borrow that is not well-modeled by Rust (like passing a pointer to the value to FFI).

这篇关于谁借了一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆