Skip to content

Commit 73a56d9

Browse files
committed
Update rust string note: matching and split by space
1 parent e80f8b1 commit 73a56d9

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

notes/language/rust/string.md

+31-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ let keyvalue_display = format!("{}={}", key, value);
3131

3232
## Methods
3333

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. |
4143

4244
### Split
4345

@@ -57,9 +59,31 @@ into a collection (array):
5759
let key_value: Vec<&str> = line.split('=').collect();
5860
```
5961

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+
6071
### Convert to number
6172

6273
```rust
6374
let parsed: i32 = "5".parse().unwrap();
6475
let turbo_parsed = "10".parse::<i32>().unwrap();
6576
```
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+
```

notes/tool/linux/package/date.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ title: Date
88
date OPTIONS FORMAT
99
```
1010

11-
| Option | Description |
12-
| -- | -- |
11+
| Option | Description |
12+
| ------------- | ---------------------------------- |
1313
| `-d` `--date` | Use specified date instead of now. |
1414

1515
## Examples

0 commit comments

Comments
 (0)