生锈:一生检查借来的借用

发布于 2025-01-22 02:25:26 字数 1088 浏览 3 评论 0原文

我在Follwoing代码方面遇到了麻烦...

use std::collections::BTreeSet;
use maybe_owned::MaybeOwned;

struct Thing<'a, T> {
    set: BTreeSet<MaybeOwned<'a, T>>
}

impl<'a, T: Ord> Thing<'a, T> {
    fn take(&mut self, x: T){
        let y = self.set.take(&MaybeOwned::Borrowed(&x));
    }
}

给出了编译器错误

error[E0597]: `x` does not live long enough
  --> src/main.rs:10:53
   |
8  | impl<'a, T: Ord> Thing<'a, T> {
   |      -- lifetime `'a` defined here
9  |     fn take(&mut self, x: T){
10 |         let y = self.set.take(&MaybeOwned::Borrowed(&x));
   |                 ------------------------------------^^--
   |                 |                                   |
   |                 |                                   borrowed value does not live long enough
   |                 argument requires that `x` is borrowed for `'a`
11 |     }
   |     - `x` dropped here while still borrowed

,但是当时显然没有借用X,因为可能所拥有的范围已经脱离了范围,因此封闭的借款已脱离范围。

我该如何告诉生锈编译器这很好?

I'm having trouble with the follwoing code...

use std::collections::BTreeSet;
use maybe_owned::MaybeOwned;

struct Thing<'a, T> {
    set: BTreeSet<MaybeOwned<'a, T>>
}

impl<'a, T: Ord> Thing<'a, T> {
    fn take(&mut self, x: T){
        let y = self.set.take(&MaybeOwned::Borrowed(&x));
    }
}

gives the compiler error

error[E0597]: `x` does not live long enough
  --> src/main.rs:10:53
   |
8  | impl<'a, T: Ord> Thing<'a, T> {
   |      -- lifetime `'a` defined here
9  |     fn take(&mut self, x: T){
10 |         let y = self.set.take(&MaybeOwned::Borrowed(&x));
   |                 ------------------------------------^^--
   |                 |                                   |
   |                 |                                   borrowed value does not live long enough
   |                 argument requires that `x` is borrowed for `'a`
11 |     }
   |     - `x` dropped here while still borrowed

However x is clearly not borrowed at that point because the MaybeOwned has fallen out of scope and therefore the enclosed borrow has fallen out of scope.

How can I tell the rust compiler that this is fine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2025-01-29 02:25:26

问题在于,虽然临时可能没有那么长的时间,但这并不重要,因为它隐含地可以lt;'a,t&gt;。这意味着x必须至少只有'a,但事实并非如此。临时可能的事实不会与借用检查器无关。

btreeset :: take()的第二个参数是&amp; q,其中集合自己的t实现borrow&lt; q&gt;可能拥有的&lt;'a,t&gt;不实现borrow&lt; lt;'b,t&gt;&gt;&gt;其中'b:'a,'a ,但是,所有t&amp; t实现借用&lt; t&gt;得益于毯子实现;'_ _,t&gt; ,唯一满足t:borrow&lt; q&gt;约束的生命周期是&amp; and a,也许是'a,t&gt; - 因此,可以推断为的临时的寿命参数为'a。这是可以满足特质界限的唯一方法。

值得庆幸的是,这都不重要,因为可能被列为'_,t&gt;实现borrow&lt; t&gt;,这意味着您只需提供&amp; t :

let y = self.set.take(&x);

The problem is that, while the temporary MaybeOwned doesn't live that long, it doesn't matter because it's implicitly MaybeOwned<'a, T>. This means x must live at least as long as 'a but it doesn't. The fact that the temporary MaybeOwned won't live that long isn't relevant to the borrow checker.

BTreeSet::take()'s second argument is a &Q, where the set's own T implements Borrow<Q>. MaybeOwned<'a, T> doesn't implement Borrow<MaybeOwned<'b, T>> where 'b: 'a, but all T and &T implement Borrow<T> thanks to a blanket implementation, so given an argument typed &MaybeOwned<'_, T>, the only lifetime that satisfies the T: Borrow<Q> constraint is &MaybeOwned<'a, T> -- thus, the lifetime parameter of the temporary MaybeOwned is inferred to be 'a. That's the only way the trait bounds can be satisfied.

Thankfully, none of this matters, because MaybeOwned<'_, T> implements Borrow<T>, which means you can just supply a &T:

let y = self.set.take(&x);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文