将对对象的引用的 vec 存储在另一个数据结构中
Store a vec of references to objects in another data structure
编译以下代码时:
pub enum NodeType {
None,
Node(Box<Node>),
}
pub struct Node {
next: NodeType,
}
impl Node {
fn traverse_recursively<F>(&self, depth: usize, f: &mut F)
where
F: FnMut(&Node, usize),
{
f(self, depth);
match &self.next {
NodeType::None => {}
NodeType::Node(node) => {
node.traverse_recursively(depth + 1, f);
}
}
}
pub fn visit_all<F>(&self, f: &mut F)
where
F: FnMut(&Node, usize),
{
self.traverse_recursively(1, f);
}
}
pub fn create_small_recursive_structure() -> Node {
Node {
next: NodeType::Node(Box::new(Node {
next: NodeType::Node(Box::new(Node { next: NodeType::None })),
})),
}
}
#[test]
fn test_so() {
let parent = create_small_recursive_structure();
let mut visited = Vec::new();
parent.visit_all(&mut |node, depth| {
visited.push((node, depth));
});
}
编译器给我以下错误:
error[E0521]: borrowed data escapes outside of closure
--> src/so_question.rs:50:9
|
47 | let mut visited = Vec::new();
| ----------- `visited` declared here, outside of the closure body
48 |
49 | parent.visit_all(&mut |node, depth| {
| ---- `node` is a reference that is only valid in the closure body
50 | visited.push((node, depth));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `node` escapes the closure body here
我发现了一个类似的问题,但解决方案对我没有帮助。 IE。我的闭包参数已经没有类型,我已经通过添加和删除类型进行了试验,但似乎没有帮助。
我需要做什么才能临时存储对树结构中节点的引用向量?目的是让向量的寿命比节点结构的寿命短。添加一对额外的括号来为编译器强调这一点没有帮助。
谢谢!
您需要指定生命周期,以便编译器知道引用的生命周期与 self
相同:
pub enum NodeType {
None,
Node(Box<Node>),
}
pub struct Node {
next: NodeType,
}
impl Node {
fn traverse_recursively<'s, F>(&'s self, depth: usize, f: &mut F)
where
F: FnMut(&'s Node, usize),
{
f(self, depth);
match &self.next {
NodeType::None => {}
NodeType::Node(node) => {
node.traverse_recursively(depth + 1, f);
}
}
}
pub fn visit_all<'s, F>(&'s self, f: &mut F)
where
F: FnMut(&'s Node, usize),
{
self.traverse_recursively(1, f);
}
}
pub fn create_small_recursive_structure() -> Node {
Node {
next: NodeType::Node(Box::new(Node {
next: NodeType::Node(Box::new(Node { next: NodeType::None })),
})),
}
}
#[test]
fn test_so() {
let parent = create_small_recursive_structure();
let mut visited = Vec::new();
parent.visit_all(&mut |node: &Node, depth| {
visited.push((node, depth));
});
}
编译以下代码时:
pub enum NodeType {
None,
Node(Box<Node>),
}
pub struct Node {
next: NodeType,
}
impl Node {
fn traverse_recursively<F>(&self, depth: usize, f: &mut F)
where
F: FnMut(&Node, usize),
{
f(self, depth);
match &self.next {
NodeType::None => {}
NodeType::Node(node) => {
node.traverse_recursively(depth + 1, f);
}
}
}
pub fn visit_all<F>(&self, f: &mut F)
where
F: FnMut(&Node, usize),
{
self.traverse_recursively(1, f);
}
}
pub fn create_small_recursive_structure() -> Node {
Node {
next: NodeType::Node(Box::new(Node {
next: NodeType::Node(Box::new(Node { next: NodeType::None })),
})),
}
}
#[test]
fn test_so() {
let parent = create_small_recursive_structure();
let mut visited = Vec::new();
parent.visit_all(&mut |node, depth| {
visited.push((node, depth));
});
}
编译器给我以下错误:
error[E0521]: borrowed data escapes outside of closure
--> src/so_question.rs:50:9
|
47 | let mut visited = Vec::new();
| ----------- `visited` declared here, outside of the closure body
48 |
49 | parent.visit_all(&mut |node, depth| {
| ---- `node` is a reference that is only valid in the closure body
50 | visited.push((node, depth));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `node` escapes the closure body here
我发现了一个类似的问题
我需要做什么才能临时存储对树结构中节点的引用向量?目的是让向量的寿命比节点结构的寿命短。添加一对额外的括号来为编译器强调这一点没有帮助。
谢谢!
您需要指定生命周期,以便编译器知道引用的生命周期与 self
相同:
pub enum NodeType {
None,
Node(Box<Node>),
}
pub struct Node {
next: NodeType,
}
impl Node {
fn traverse_recursively<'s, F>(&'s self, depth: usize, f: &mut F)
where
F: FnMut(&'s Node, usize),
{
f(self, depth);
match &self.next {
NodeType::None => {}
NodeType::Node(node) => {
node.traverse_recursively(depth + 1, f);
}
}
}
pub fn visit_all<'s, F>(&'s self, f: &mut F)
where
F: FnMut(&'s Node, usize),
{
self.traverse_recursively(1, f);
}
}
pub fn create_small_recursive_structure() -> Node {
Node {
next: NodeType::Node(Box::new(Node {
next: NodeType::Node(Box::new(Node { next: NodeType::None })),
})),
}
}
#[test]
fn test_so() {
let parent = create_small_recursive_structure();
let mut visited = Vec::new();
parent.visit_all(&mut |node: &Node, depth| {
visited.push((node, depth));
});
}