Browse Source

README, lib.rs: Use ? operator in documentation

Johannes Hofmann 5 years ago
parent
commit
df3d8b2a21
2 changed files with 35 additions and 37 deletions
  1. 27
    33
      README.md
  2. 8
    4
      src/lib.rs

+ 27
- 33
README.md View File

28
 Here's a simple example that counts all the ways in a file:
28
 Here's a simple example that counts all the ways in a file:
29
 
29
 
30
 ```rust
30
 ```rust
31
-extern crate osmpbf;
32
-
33
-use osmpbf::*;
31
+use osmpbf::{ElementReader, Element};
34
 
32
 
35
-fn main() {
36
-    let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
37
-    let mut ways = 0_u64;
33
+let reader = ElementReader::from_path("tests/test.osm.pbf")?;
34
+let mut ways = 0_u64;
38
 
35
 
39
-    // Increment the counter by one for each way.
40
-    reader.for_each(|element| {
41
-        if let Element::Way(_) = element {
42
-            ways += 1;
43
-        }
44
-    }).unwrap();
36
+// Increment the counter by one for each way.
37
+reader.for_each(|element| {
38
+    if let Element::Way(_) = element {
39
+        ways += 1;
40
+    }
41
+})?;
45
 
42
 
46
-    println!("Number of ways: {}", ways);
47
-}
43
+println!("Number of ways: {}", ways);
48
 ```
44
 ```
49
 
45
 
50
 In this second example, we also count the ways but make use of all cores by
46
 In this second example, we also count the ways but make use of all cores by
51
 decoding the file in parallel:
47
 decoding the file in parallel:
52
 
48
 
53
 ```rust
49
 ```rust
54
-use osmpbf::*;
55
-
56
-fn main() {
57
-    let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
58
-
59
-    // Count the ways
60
-    let ways = reader.par_map_reduce(
61
-        |element| {
62
-            match element {
63
-                Element::Way(_) => 1,
64
-                _ => 0,
65
-            }
66
-        },
67
-        || 0_u64,      // Zero is the identity value for addition
68
-        |a, b| a + b   // Sum the partial results
69
-    ).unwrap();
70
-
71
-    println!("Number of ways: {}", ways);
72
-}
50
+use osmpbf::{ElementReader, Element};
51
+
52
+let reader = ElementReader::from_path("tests/test.osm.pbf")?;
53
+
54
+// Count the ways
55
+let ways = reader.par_map_reduce(
56
+    |element| {
57
+        match element {
58
+            Element::Way(_) => 1,
59
+            _ => 0,
60
+        }
61
+    },
62
+    || 0_u64,      // Zero is the identity value for addition
63
+    |a, b| a + b   // Sum the partial results
64
+)?;
65
+
66
+println!("Number of ways: {}", ways);
73
 ```
67
 ```
74
 
68
 
75
 ## The PBF format
69
 ## The PBF format

+ 8
- 4
src/lib.rs View File

24
 ```rust
24
 ```rust
25
 use osmpbf::{ElementReader, Element};
25
 use osmpbf::{ElementReader, Element};
26
 
26
 
27
-let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
27
+let reader = ElementReader::from_path("tests/test.osm.pbf")?;
28
 let mut ways = 0_u64;
28
 let mut ways = 0_u64;
29
 
29
 
30
 // Increment the counter by one for each way.
30
 // Increment the counter by one for each way.
32
     if let Element::Way(_) = element {
32
     if let Element::Way(_) = element {
33
         ways += 1;
33
         ways += 1;
34
     }
34
     }
35
-}).unwrap();
35
+})?;
36
 
36
 
37
 println!("Number of ways: {}", ways);
37
 println!("Number of ways: {}", ways);
38
+# assert_eq!(ways, 1);
39
+# Ok::<(), std::io::Error>(())
38
 ```
40
 ```
39
 
41
 
40
 ## Example: Count ways in parallel
42
 ## Example: Count ways in parallel
45
 ```rust
47
 ```rust
46
 use osmpbf::{ElementReader, Element};
48
 use osmpbf::{ElementReader, Element};
47
 
49
 
48
-let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
50
+let reader = ElementReader::from_path("tests/test.osm.pbf")?;
49
 
51
 
50
 // Count the ways
52
 // Count the ways
51
 let ways = reader.par_map_reduce(
53
 let ways = reader.par_map_reduce(
57
     },
59
     },
58
     || 0_u64,      // Zero is the identity value for addition
60
     || 0_u64,      // Zero is the identity value for addition
59
     |a, b| a + b   // Sum the partial results
61
     |a, b| a + b   // Sum the partial results
60
-).unwrap();
62
+)?;
61
 
63
 
62
 println!("Number of ways: {}", ways);
64
 println!("Number of ways: {}", ways);
65
+# assert_eq!(ways, 1);
66
+# Ok::<(), std::io::Error>(())
63
 ```
67
 ```
64
 */
68
 */
65
 
69