Skip to content

Commit

Permalink
Update rust string note: matching and split by space
Browse files Browse the repository at this point in the history
  • Loading branch information
Yutsuten committed Jan 5, 2025
1 parent e80f8b1 commit 73a56d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
38 changes: 31 additions & 7 deletions notes/language/rust/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ let keyvalue_display = format!("{}={}", key, value);

## Methods

| Method | Description |
| --- | --- |
| `get` | Safer way to access index of the string. |
| `is_empty` | Returns `true` if the string has length of zero. |
| `len` | Get the length of the string in number of bytes. |
| `lines` | An iterator over the lines of a string, as string slices. |
| `trim` | Returns a string slice with leading and trailing whitespace removed (includes `\n`). |
| Method | Description |
| ------------------ | ------------------------------------------------------------------------------------ |
| `get` | Safer way to access index of the string. |
| `is_empty` | Returns `true` if the string has length of zero. |
| `len` | Get the length of the string in number of bytes. |
| `lines` | An iterator over the lines of a string, as string slices. |
| `trim` | Returns a string slice with leading and trailing whitespace removed (includes `\n`). |
| `split` | Splits a string slice by one or more characters. |
| `split_whitespace` | Splits a string slice by any amount of whitespace. |

### Split

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

Split by space and remove empty values:

```rust
let values: Vec<String> = line
.split_whitespace()
.map(|s| s.to_owned())
.collect();
```

### Convert to number

```rust
let parsed: i32 = "5".parse().unwrap();
let turbo_parsed = "10".parse::<i32>().unwrap();
```

## Matching

Given an owned `String`,
match its values with:

```rust
match text.as_str() {
"true" => true,
"false" => false,
_ => panic!("Expected true or false");
}
```
4 changes: 2 additions & 2 deletions notes/tool/linux/package/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ title: Date
date OPTIONS FORMAT
```

| Option | Description |
| -- | -- |
| Option | Description |
| ------------- | ---------------------------------- |
| `-d` `--date` | Use specified date instead of now. |

## Examples
Expand Down

0 comments on commit 73a56d9

Please sign in to comment.