Hi! I began to learn Rust and can't understand some basic principles.
I try to build xml tree. This is my main code
extern crate xml;
mod model;
use std::fs::File;
use std::io::BufReader;
use std::string::String;
use parser::xml::reader::{EventReader, XmlEvent};
pub fn build_tree(file_name: &String) -> model::Node {
let file = File::open(file_name).unwrap();
let file = BufReader::new(file);
let parser = EventReader::new(file);
let mut root = model::Node::new();
let mut nodes = Vec::<&mut model::Node>::new();
for e in parser {
match e {
c @ Ok(XmlEvent::StartElement { .. }) => {
let mut new_node = model::Node::new();
let add_node: &mut model::Node;
{
add_node = match nodes.last_mut() {
Some(ref mut n) =>
n.add_child(new_node),
None =>
root.add_child(new_node)
};
}
nodes.push(add_node)
},
Ok(XmlEvent::EndElement { .. }) => {
nodes.pop();
},
Err(e) => {
println!("Error: {}", e);
break;
}
_ => {}
}
}
return root;
}
nodes is a vector contained nodes chain in depth. root - is a root node of tree. All nodes should be owned by root, but "nodes" should borrow links to nodes for find current node's parent.
And I get huge amount of borrow errors during compilation.
warning: unused variable: `c`
--> src/parser/mod.rs:19:13
|
19 | c @ Ok(XmlEvent::StartElement { .. }) => {
| ^ help: consider using `_c` instead
|
= note: #[warn(unused_variables)] on by default
error[E0597]: borrowed value does not live long enough
--> src/parser/mod.rs:24:30
|
24 | Some(ref mut n) =>
| ^^^^^^^^^ borrowed value does not live long enough
...
28 | };
| - borrowed value dropped here while still borrowed
...
44 | }
| - borrowed value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
error[E0499]: cannot borrow `nodes` as mutable more than once at a time
--> src/parser/mod.rs:23:38
|
23 | add_node = match nodes.last_mut() {
| ^^^^^ mutable borrow starts here in previous iteration of loop
...
44 | }
| - mutable borrow ends here
error[E0499]: cannot borrow `root` as mutable more than once at a time
--> src/parser/mod.rs:27:29
|
27 | root.add_child(new_node)
| ^^^^ mutable borrow starts here in previous iteration of loop
...
44 | }
| - mutable borrow ends here
error[E0499]: cannot borrow `nodes` as mutable more than once at a time
--> src/parser/mod.rs:30:17
|
23 | add_node = match nodes.last_mut() {
| ----- first mutable borrow occurs here
...
30 | nodes.push(add_node)
| ^^^^^ second mutable borrow occurs here
...
44 | }
| - first borrow ends here
error[E0499]: cannot borrow `nodes` as mutable more than once at a time
--> src/parser/mod.rs:33:17
|
23 | add_node = match nodes.last_mut() {
| ----- first mutable borrow occurs here
...
33 | nodes.pop();
| ^^^^^ second mutable borrow occurs here
...
44 | }
| - first borrow ends here
error[E0505]: cannot move out of `root` because it is borrowed
--> src/parser/mod.rs:43:12
|
27 | root.add_child(new_node)
| ---- borrow of `root` occurs here
...
43 | return root;
| ^^^^ move out of `root` occurs here
I think I don't really understand borrow scopes. Can somebody explain? Or what it be useful to read?
Thank you!