生锈:一生检查借来的借用
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,虽然临时
可能
没有那么长的时间,但这并不重要,因为它隐含地可以
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 :
The problem is that, while the temporary
MaybeOwned
doesn't live that long, it doesn't matter because it's implicitlyMaybeOwned<'a, T>
. This meansx
must live at least as long as'a
but it doesn't. The fact that the temporaryMaybeOwned
won't live that long isn't relevant to the borrow checker.BTreeSet::take()
's second argument is a&Q
, where the set's ownT
implementsBorrow<Q>
.MaybeOwned<'a, T>
doesn't implementBorrow<MaybeOwned<'b, T>>
where'b: 'a
, but allT
and&T
implementBorrow<T>
thanks to a blanket implementation, so given an argument typed&MaybeOwned<'_, T>
, the only lifetime that satisfies theT: Borrow<Q>
constraint is&MaybeOwned<'a, T>
-- thus, the lifetime parameter of the temporaryMaybeOwned
is inferred to be'a
. That's the only way the trait bounds can be satisfied.Thankfully, none of this matters, because
MaybeOwned<'_, T>
implementsBorrow<T>
, which means you can just supply a&T
: