You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation2/B07-File-Handling.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -161,3 +161,40 @@ cargo run
161
161
```bash
162
162
Created a file data.txt
163
163
```
164
+
165
+
Here, we import `std::fs::File` and `std::io::Write` modules for writing to a file. We first create a file `data.txt` with the `File::create("data.txt")` method and bind it to a mutable variable `data_file`.
166
+
167
+
After we create a file, we write to the file using the `write()` method with the content `"Hello, World!"`.
168
+
169
+
____
170
+
171
+
### Removing a File in Rust
172
+
173
+
To remove or delete a file in Rust, we can use the remove_file() method from the std::fs module.
174
+
175
+
For example,
176
+
177
+
```rust
178
+
use std::fs;
179
+
180
+
fn main() {
181
+
// Remove a file
182
+
fs::remove_file("data.txt").expect("could not remove file");
0 commit comments