- 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.
推薦閱讀
- OpenDaylight Cookbook
- CockroachDB權(quán)威指南
- 樂學(xué)Web編程:網(wǎng)站制作不神秘
- Access 2010數(shù)據(jù)庫基礎(chǔ)與應(yīng)用項(xiàng)目式教程(第3版)
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- Kotlin編程實(shí)戰(zhàn):創(chuàng)建優(yōu)雅、富于表現(xiàn)力和高性能的JVM與Android應(yīng)用程序
- SQL基礎(chǔ)教程(第2版)
- Offer來了:Java面試核心知識點(diǎn)精講(框架篇)
- Splunk Essentials
- 面向?qū)ο蠓治雠c設(shè)計(jì)(第3版)
- Learning Perforce SCM
- Spring Microservices
- 威脅建模:設(shè)計(jì)和交付更安全的軟件
- Mastering Swift 4(Fourth Edition)
- 3ds Max瘋狂設(shè)計(jì)學(xué)院