In this program, I want to use the contents of the file outside the if statement. This code works, but I get the warning "value assigned to contents is never read". How can I change it so I don't get the warning, but keep the functionality?
use std::fs;
use std::io;
fn main() {
let filename = "example.txt";
fn read_file_contents(filename: &str) -> Result<String, io::Error> {
let fcontents = fs::read_to_string(filename)?;
Ok(fcontents)
}
let mut contents = String::new();
if let Ok(fcontents) = read_file_contents(filename) {
contents = fcontents;
} else {
panic!("Error reading file {}", filename);
}
println!("File contents: {}", contents);
}