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

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.

主站蜘蛛池模板: 婺源县| 海宁市| 高要市| 福州市| 潜山县| 蒙自县| 尼木县| 泊头市| 永胜县| 深州市| 东光县| 德清县| 库伦旗| 兴文县| 长沙县| 汤原县| 阿克苏市| 贵州省| 五大连池市| 梁平县| 伊春市| 马山县| 绵阳市| 普安县| 乐都县| 永安市| 兰考县| 金阳县| 琼海市| 平泉县| 盖州市| 务川| 陕西省| 禹州市| 涟源市| 黄梅县| 晋州市| 湾仔区| 屯门区| 天台县| 周至县|