@@ -31,13 +31,15 @@ let keyvalue_display = format!("{}={}", key, value);
31
31
32
32
## Methods
33
33
34
- | Method | Description |
35
- | --- | --- |
36
- | ` get ` | Safer way to access index of the string. |
37
- | ` is_empty ` | Returns ` true ` if the string has length of zero. |
38
- | ` len ` | Get the length of the string in number of bytes. |
39
- | ` lines ` | An iterator over the lines of a string, as string slices. |
40
- | ` trim ` | Returns a string slice with leading and trailing whitespace removed (includes ` \n ` ). |
34
+ | Method | Description |
35
+ | ------------------ | ------------------------------------------------------------------------------------ |
36
+ | ` get ` | Safer way to access index of the string. |
37
+ | ` is_empty ` | Returns ` true ` if the string has length of zero. |
38
+ | ` len ` | Get the length of the string in number of bytes. |
39
+ | ` lines ` | An iterator over the lines of a string, as string slices. |
40
+ | ` trim ` | Returns a string slice with leading and trailing whitespace removed (includes ` \n ` ). |
41
+ | ` split ` | Splits a string slice by one or more characters. |
42
+ | ` split_whitespace ` | Splits a string slice by any amount of whitespace. |
41
43
42
44
### Split
43
45
@@ -57,9 +59,31 @@ into a collection (array):
57
59
let key_value : Vec <& str > = line . split ('=' ). collect ();
58
60
```
59
61
62
+ Split by space and remove empty values:
63
+
64
+ ``` rust
65
+ let values : Vec <String > = line
66
+ . split_whitespace ()
67
+ . map (| s | s . to_owned ())
68
+ . collect ();
69
+ ```
70
+
60
71
### Convert to number
61
72
62
73
``` rust
63
74
let parsed : i32 = " 5" . parse (). unwrap ();
64
75
let turbo_parsed = " 10" . parse :: <i32 >(). unwrap ();
65
76
```
77
+
78
+ ## Matching
79
+
80
+ Given an owned ` String ` ,
81
+ match its values with:
82
+
83
+ ``` rust
84
+ match text . as_str () {
85
+ " true" => true ,
86
+ " false" => false ,
87
+ _ => panic! (" Expected true or false" );
88
+ }
89
+ ```
0 commit comments