mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-06 19:06:41 +00:00
Add files notes
This commit is contained in:
parent
c7e366dfee
commit
2187ac87be
1 changed files with 30 additions and 0 deletions
30
Rust/Rust.md
30
Rust/Rust.md
|
@ -654,3 +654,33 @@ let v = vec![
|
||||||
Enum::Text("TEST")
|
Enum::Text("TEST")
|
||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Files
|
||||||
|
|
||||||
|
### Reading Files
|
||||||
|
|
||||||
|
```rs
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
let contents: Vec<u8> = fs::read("path/to/file").unwrap_or_default();
|
||||||
|
let contents: String = fs::read_to_string("path/to/file").unwrap_or_default();
|
||||||
|
|
||||||
|
contents.lines(); // iterator over text lines
|
||||||
|
```
|
||||||
|
|
||||||
|
### Writing Files
|
||||||
|
|
||||||
|
```rs
|
||||||
|
use std::fs;
|
||||||
|
use std::io::Write; // write trait
|
||||||
|
// or
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
let contents: [u8] = /* */;
|
||||||
|
let contents = String::from(/* */);
|
||||||
|
|
||||||
|
fs::write("path/to/file", contents);
|
||||||
|
|
||||||
|
let mut file = fs::OpenOptions::new().append(true).open("path/to/file").unwrap();
|
||||||
|
file.write(b"appended text"); // write wants an [u8]
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue