I am very new to Rust, and have the question, how to write a string to a file in Rust. There are plenty of tutorials and documentations out there, how to write an &str type to file, but no tutorial how to write a String type to files. I wrote this code by myself and it compiles, but I always get an "Bad file descriptor (os error 9)". I'm on Linux(Manjaro). I'm very thankfully for every help I will get.
//Import
use std::fs::File;
use std::io::*;
//Mainfunction
fn main() {
//Programm startet hier | program starts here
println!("Program started...");
// Lesen des Files, createn des Buffers | reading file createing buffer
let mut testfile = File::open("/home/julian/.rust_test/test_0.txt").unwrap();
let mut teststring = String::from("lol");
//Vom File auf den Buffer schreiben | writing from file to buffer
testfile.read_to_string(&mut teststring).unwrap();
//Buffer ausgeben | print to buffer
println!("teststring: {}", teststring);
// Neue Variable deklarieren | declare new variable
let msg = String::from("Writetest tralalalal.");
// msg an ursprünglichen String anhängen | append msg to string
teststring.push_str(&msg);
println!("teststring: {}", teststring);
// Neuen String nach File schreiben | write new sting to file
let eg = testfile.write_all(&teststring.as_bytes());
match eg {
Ok(()) => println!("OK"),
Err(e) => println!("{}",e)
}
println!("Fertig")
}