append to file rust
To append content to a file in Rust, you can use the std::fs::OpenOptions
struct along with the append
method. Here's an example:
use std::fs::OpenOptions;
use std::io::Write;
fn main() {
// Open the file in append mode
let file_path = "path/to/your/file.txt";
let mut file = OpenOptions::new()
.append(true)
.open(file_path)
.expect("Failed to open file");
// Write content to the file
let content = "This is the content to append";
file.write_all(content.as_bytes())
.expect("Failed to write to file");
}
In this example, we first create an OpenOptions
instance and set the append
flag to true
. Then, we open the file using the open
method, passing in the file path. Finally, we use the write_all
method to write the content to the file.
Please make sure to replace "path/to/your/file.txt"
with the actual path to your file.