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

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.

主站蜘蛛池模板: 上蔡县| 宿松县| 永靖县| 通山县| 淄博市| 贞丰县| 宁明县| 阿瓦提县| 长武县| 桐城市| 田阳县| 达拉特旗| 威海市| 田林县| 永胜县| 志丹县| 绍兴市| 兴义市| 峨眉山市| 盐亭县| 宿州市| 大石桥市| 大新县| 仙游县| 隆安县| 浮梁县| 共和县| 西乌珠穆沁旗| 乐山市| 蚌埠市| 丽江市| 昌江| 肇东市| 建阳市| 石首市| 洞口县| 宿州市| 三都| 福州市| 崇仁县| 淅川县|