Skip to content

Commit ac92aa0

Browse files
committed
Update some rust notes
1 parent d285ccc commit ac92aa0

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

notes/language/rust/crates/std.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,32 @@ Other common methods:
202202
Using `keys().collect()` may be useful to `.sort()` and iterate the `HashMap` in the same order,
203203
because by default the order can change each run.
204204

205-
## Process
205+
## External command
206206

207-
Run external commands.
207+
Run external commands and get `stdout` as UTF8 `String`:
208208

209209
```rust
210-
process::Command::new("swaymsg").args(["output", "*", "bg", wallpaper.as_str(), "fill"]).output().unwrap();
210+
let output = process::Command::new("echo").args(["Hello World!"]).output().unwrap();
211+
let text = String::from_utf8(output.stdout);
212+
```
213+
214+
To get `stdout` in real time, a buffer is needed ([Rust Cookbook](https://rust-lang-nursery.github.io/rust-cookbook/os/external.html#continuously-process-child-process-outputs)):
215+
216+
```rust
217+
use std::process::{Command, Stdio};
218+
use std::io::{BufRead, BufReader};
219+
220+
fn main() {
221+
let stdout = Command::new("ffmpeg")
222+
.args(ffmpeg_args)
223+
.stdout(Stdio::piped())
224+
.spawn()
225+
.unwrap()
226+
.stdout
227+
.unwrap();
228+
let reader = BufReader::new(stdout);
229+
reader
230+
.lines()
231+
.for_each(|line| println!("{}", line.unwrap()));
232+
}
211233
```

notes/language/rust/numbers.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@ ref: https://forkful.ai/en/rust/numbers/rounding-numbers/
55

66
## Methods
77

8-
| Method | Description |
9-
| --- | --- |
10-
| `round` | Round to nearest whole number. |
11-
| `floor` | Largest integer less than or equal to number. |
12-
| `ceil` | Smallest integer greater than or equal to number. |
13-
| `trunc` | Integer part without fractional digits. |
8+
| Method | Description |
9+
| ------- | ------------------------------------------------- |
10+
| `round` | Round to nearest whole number. |
11+
| `floor` | Largest integer less than or equal to number. |
12+
| `ceil` | Smallest integer greater than or equal to number. |
13+
| `trunc` | Integer part without fractional digits. |
1414

1515
Example:
1616

1717
```rust
1818
let pi = 3.141592;
1919
let round_pi = pi.round();
2020
```
21+
22+
## Update reference
23+
24+
If updating a number in a function that received it as `&mut`:
25+
26+
```rust
27+
fn update_counter(&mut count) {
28+
*count += 1;
29+
}
30+
```

notes/language/rust/string.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const GLOBAL_STR: &'static str = "Available globally";
1313
let mystr = "Hello World";
1414
let string_type1 = String::from(mystr);
1515
let string_type2 = mystr.to_owned(); // `.to_string()` also ok
16-
let back_to_str = string_type1.to_str();
16+
let back_to_str = string_type1.as_str();
1717
let raw_string = r#"
1818
{
1919
"name": "John Doe",
@@ -40,6 +40,8 @@ let keyvalue_display = format!("{}={}", key, value);
4040
| `trim` | Returns a string slice with leading and trailing whitespace removed (includes `\n`). |
4141
| `split` | Splits a string slice by one or more characters. |
4242
| `split_whitespace` | Splits a string slice by any amount of whitespace. |
43+
| `clear` | Truncates this `String`, removing all contents. |
44+
| `push_str` | Appends a given string slice onto the end of this `String`. |
4345

4446
### Split
4547

@@ -87,3 +89,18 @@ match text.as_str() {
8789
_ => panic!("Expected true or false");
8890
}
8991
```
92+
93+
## Update without assign
94+
95+
If updating a `String` in a function that received it as `&mut`,
96+
reassigning do not work.
97+
98+
```rust
99+
fn update_string(&mut s) {
100+
// Instead of:
101+
// s = String::from("New string");
102+
// Do:
103+
s.clear();
104+
s.push_str("New string");
105+
}
106+
```

notes/tool/anki.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ first create and activate a virtual environment:
3030
python -m venv --system-site-packages .venv
3131
```
3232

33-
The packages `pyqt6 pyqt6-webengine` will also be needed.
33+
The package `python-pyqt6-webengine` will also be needed.
3434
Prefer your system's package manager to install it.
3535

3636
```shell

0 commit comments

Comments
 (0)