mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-08-14 14:26:56 +00:00
- file_io3: replace the hardcoded expected file size (117) with SAMPLE_TEXT.len(), so the check is derived from the actual content instead of an unexplained magic number. - file_io3: use create_dir_all instead of create_dir, and add a sample_file_path() helper shared by main/create_required_files/ file_cleanup to avoid three separate ad-hoc path constructions. - file_io1/file_io2: hoist the sample text into a SAMPLE_TEXT constant used by both the exercise and its solution, removing duplicated literals. No behavioral change: all three solutions were rebuilt and run locally, producing identical output (file size still 117, permissions still non-readonly) with no leftover files.
54 lines
1.2 KiB
Rust
54 lines
1.2 KiB
Rust
use std::fs;
|
|
use std::path::Path;
|
|
|
|
const TEST_FILE_NAME: &str = "SampleTextFile.txt";
|
|
const SAMPLE_TEXT: &str = "This is the file content.";
|
|
|
|
fn main() -> Result<(), std::io::Error> {
|
|
create_required_files()?;
|
|
|
|
let read_str_result = fs::read_to_string(TEST_FILE_NAME);
|
|
|
|
match read_str_result {
|
|
Ok(contents) => {
|
|
// TODO : What would be the expected text ?
|
|
assert_eq!(, contents);
|
|
}
|
|
Err(err) => {
|
|
eprintln!("File read error. {}", err);
|
|
}
|
|
}
|
|
|
|
file_cleanup()?;
|
|
Ok(())
|
|
}
|
|
|
|
fn create_required_files() -> Result<(), std::io::Error> {
|
|
let file_path = Path::new(TEST_FILE_NAME);
|
|
|
|
if !file_path.exists() {
|
|
fs::write(file_path, SAMPLE_TEXT)?;
|
|
} else {
|
|
println!("File already exist.");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn file_cleanup() -> Result<(), std::io::Error> {
|
|
let file_path = Path::new(TEST_FILE_NAME);
|
|
|
|
if file_path.exists() {
|
|
fs::remove_file(file_path).inspect(|_| {
|
|
println!("Test file {} deleted.", TEST_FILE_NAME);
|
|
})?;
|
|
} else {
|
|
println!(
|
|
"No cleanup necessary since {} not exist.",
|
|
file_path.display()
|
|
);
|
|
}
|
|
|
|
Ok(())
|
|
}
|