I'm attempting to get a handle on reading and writing from and to files and one of the sources I found uses the write_all() method. Right now all I want to do is write some text to a file. My code works well when, using write_all()
, I pass my text in between quotes in the write_all() method. However, my goal is to allow the user to enter text of his/her choice, store that text in a String, and then pass the string on to write_all()
to save it in the file. I keep getting the error:
buffer.write_all(savetext.as_bytes()).expect("Cannot write data to file.");
| ^^^^^^^^ help: a local variable with a similar name exists: `savetxt`
I've tried multiple ways of passing savetxt
to write_all()
, but keep getting the same error. So.....Help!??
Here is my current code:
use std::io::prelude::*;
use std::fs::File;
fn main() -> std::io::Result<()> {
let fname = "fefifofum.txt";
let mut buffer = File::create(&fname).expect("Cannot create file.");
let savetxt: String = String::from("This is the day that the Lord hath made!");
buffer.write_all(savetext.as_bytes()).expect("Cannot write data to file.");
Ok(())
}
And here is the base code I started with:
fn main() -> std::io::Result<()> {
let mut buffer = File::create("foo.txt")?;
buffer.write_all(b"some bytes")?;
Ok(())
}
That base code comes from here.
Thanks for your help.