Rust 无法识别借用在循环结束时结束
Rust doesn't recognize that a borrow ends at the end of a loop
在下面的代码中,rust 编译器似乎有问题,在代码执行结束时删除了借用。即使我将 for 循环括在方括号中以使其具有自己的范围,它似乎也没有意识到借用结束。
use std::ops::RangeBounds;
use std::any::Any;
pub trait UpdateTables<T> {
fn apply_first<'a>(&mut self, table: &'a mut T) -> Box<dyn Any + 'a>;
}
struct Drain<R>
where
R: 'static + Clone + RangeBounds<usize>,
{
pub r: R,
}
impl<T, R> UpdateTables<Vec<T>> for Drain<R>
where
R: 'static + Clone + RangeBounds<usize>,
{
fn apply_first<'a>(&mut self, table: &'a mut Vec<T>) -> Box<dyn Any + 'a> {
Box::new(table.drain(self.r.clone()))
}
}
fn main() {
let mut v = vec![1, 2, 4, 5, 7, 8];
let mut d = Drain {
r: (1..),
};
println!("{:?}", &v);
for i in d.apply_first(&mut v).downcast::<std::vec::Drain<'_, i32>>().unwrap() {
println!("{:?}", i);
}
println!("{:?}", &v);
}
编译错误:
error[E0597]: `v` does not live long enough
--> src/main.rs:32:28
|
32 | for i in d.apply_first(&mut v).downcast::<std::vec::Drain<'_, i32>>().unwrap() {
| --------------^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `v` is borrowed for `'static`
...
37 | }
| - `v` dropped here while still borrowed
error[E0502]: cannot borrow `v` as immutable because it is also borrowed as mutable
--> src/main.rs:36:22
|
32 | for i in d.apply_first(&mut v).downcast::<std::vec::Drain<'_, i32>>().unwrap() {
| ---------------------
| | |
| | mutable borrow occurs here
| argument requires that `v` is borrowed for `'static`
...
36 | println!("{:?}", &v);
| ^^ immutable borrow occurs here
循环不是问题。这个简化示例中的错误是相同的:
use std::any::Any;
fn apply_first<'a, T>(table: &'a mut Vec<T>) -> Box<dyn Any + 'a> {
Box::new(table.drain(1..))
}
fn main() {
let mut v = vec![1, 2, 4, 5, 7, 8];
let _ = apply_first(&mut v);
println!("{:?}", &v);
}
error[E0597]: `v` does not live long enough
--> src/main.rs:9:25
|
9 | let _ = apply_first(&mut v);
| ------------^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `v` is borrowed for `'static`
10 | println!("{:?}", &v);
11 | }
| - `v` dropped here while still borrowed
error[E0502]: cannot borrow `v` as immutable because it is also borrowed as mutable
--> src/main.rs:10:22
|
9 | let _ = apply_first(&mut v);
| -------------------
| | |
| | mutable borrow occurs here
| argument requires that `v` is borrowed for `'static`
10 | println!("{:?}", &v);
| ^^ immutable borrow occurs here
核心问题是Any
只适用于'static
类型,所以dyn Any + 'a
没有意义,暗示'a: 'static
。我确实希望编译器能提供更多关于 'static
约束的来源的信息,但我离题了。
另请参阅:
在下面的代码中,rust 编译器似乎有问题,在代码执行结束时删除了借用。即使我将 for 循环括在方括号中以使其具有自己的范围,它似乎也没有意识到借用结束。
use std::ops::RangeBounds;
use std::any::Any;
pub trait UpdateTables<T> {
fn apply_first<'a>(&mut self, table: &'a mut T) -> Box<dyn Any + 'a>;
}
struct Drain<R>
where
R: 'static + Clone + RangeBounds<usize>,
{
pub r: R,
}
impl<T, R> UpdateTables<Vec<T>> for Drain<R>
where
R: 'static + Clone + RangeBounds<usize>,
{
fn apply_first<'a>(&mut self, table: &'a mut Vec<T>) -> Box<dyn Any + 'a> {
Box::new(table.drain(self.r.clone()))
}
}
fn main() {
let mut v = vec![1, 2, 4, 5, 7, 8];
let mut d = Drain {
r: (1..),
};
println!("{:?}", &v);
for i in d.apply_first(&mut v).downcast::<std::vec::Drain<'_, i32>>().unwrap() {
println!("{:?}", i);
}
println!("{:?}", &v);
}
编译错误:
error[E0597]: `v` does not live long enough
--> src/main.rs:32:28
|
32 | for i in d.apply_first(&mut v).downcast::<std::vec::Drain<'_, i32>>().unwrap() {
| --------------^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `v` is borrowed for `'static`
...
37 | }
| - `v` dropped here while still borrowed
error[E0502]: cannot borrow `v` as immutable because it is also borrowed as mutable
--> src/main.rs:36:22
|
32 | for i in d.apply_first(&mut v).downcast::<std::vec::Drain<'_, i32>>().unwrap() {
| ---------------------
| | |
| | mutable borrow occurs here
| argument requires that `v` is borrowed for `'static`
...
36 | println!("{:?}", &v);
| ^^ immutable borrow occurs here
循环不是问题。这个简化示例中的错误是相同的:
use std::any::Any;
fn apply_first<'a, T>(table: &'a mut Vec<T>) -> Box<dyn Any + 'a> {
Box::new(table.drain(1..))
}
fn main() {
let mut v = vec![1, 2, 4, 5, 7, 8];
let _ = apply_first(&mut v);
println!("{:?}", &v);
}
error[E0597]: `v` does not live long enough
--> src/main.rs:9:25
|
9 | let _ = apply_first(&mut v);
| ------------^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `v` is borrowed for `'static`
10 | println!("{:?}", &v);
11 | }
| - `v` dropped here while still borrowed
error[E0502]: cannot borrow `v` as immutable because it is also borrowed as mutable
--> src/main.rs:10:22
|
9 | let _ = apply_first(&mut v);
| -------------------
| | |
| | mutable borrow occurs here
| argument requires that `v` is borrowed for `'static`
10 | println!("{:?}", &v);
| ^^ immutable borrow occurs here
核心问题是Any
只适用于'static
类型,所以dyn Any + 'a
没有意义,暗示'a: 'static
。我确实希望编译器能提供更多关于 'static
约束的来源的信息,但我离题了。
另请参阅: