- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 193字
- 2021-07-02 23:07:23
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.
推薦閱讀
- 深入理解Bootstrap
- 觸·心:DT時代的大數據精準營銷
- 深入淺出Electron:原理、工程與實踐
- Java Web及其框架技術
- 差分進化算法及其高維多目標優化應用
- Responsive Web Design by Example
- C# 8.0核心技術指南(原書第8版)
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- 愛上micro:bit
- Nagios Core Administration Cookbook(Second Edition)
- Learning Nessus for Penetration Testing
- Mastering PowerCLI
- Web開發的平民英雄:PHP+MySQL
- iOS Development with Xamarin Cookbook
- VMware vSphere 5.5 Cookbook