官术网_书友最值得收藏!

Reading a file

As with a standard file, we first have to open the file and create a reader:

let file = File::open("my_xmlfile.xml").unwrap();
let reader =BufferedReader::new(file);

Next, we start the reading. Unlike a normal reader, we use EventReader. This provides a number of events (such as StartElement, EndElement, and Error), which are required for reading in from the differing nodes:

let mut xml_parser = EventReader::new(reader); 

Next, we iterate through the file, as follows:

for e in xml_parser.events() { 
     match e { 
         StartElement { name, .. } => { 
              println!("{}", name); 
         } 
         EndElement {name} => { 
             println!("{}", name); 
         } 
         Error(e) => { 
             println!("Error in file: {}", e); 
      } 
      _ => {} 
   } 
} 

In the preceding snippet, _ => {} essentially means that you don't care what is left, do something with it (in this case, the something is nothing). You will see the symbol _ quite a bit in Rust. Commonly, it is used in loops where the variable being acted on is never used, for example:

for _ in something() {...} 

We aren't going to use the iterator; we just need something to enable the iteration to go to the next value.

主站蜘蛛池模板: 盘山县| 大厂| 黄骅市| 象山县| 怀化市| 志丹县| 黔西县| 凉城县| 阿拉善右旗| 江西省| 龙陵县| 安西县| 南开区| 河南省| 象山县| 鄄城县| 肇州县| 霞浦县| 达日县| 和平县| 广丰县| 晋宁县| 高密市| 财经| 文山县| 讷河市| 海口市| 双辽市| 枝江市| 青龙| 孙吴县| 桂东县| 丹凤县| 元氏县| 闽侯县| 江口县| 安阳市| 扶风县| 连南| 新余市| 鹿邑县|