mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
Changes in solutions are reflected to the exercises
This commit is contained in:
parent
c7fd0e4a87
commit
8e7e878eab
@ -12,9 +12,8 @@ fn main() {
|
||||
// TODO : What would be the expected text ?
|
||||
assert_eq!(, contents);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file: {}", e);
|
||||
assert!(false);
|
||||
Err(_) => {
|
||||
panic!("Error reading file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,8 +21,8 @@ fn main() {
|
||||
fn create_required_files() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
|
||||
if file_path.exists() == false {
|
||||
fs::write(&file_path, "This is the file content.").unwrap();
|
||||
if !file_path.exists() {
|
||||
fs::write(file_path, "This is the file content.").unwrap();
|
||||
println!("File created.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
|
||||
use std::fs;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
use std::path::Path;
|
||||
@ -6,41 +5,41 @@ use std::path::Path;
|
||||
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||
|
||||
fn main() {
|
||||
|
||||
create_required_files();
|
||||
let input_file = fs::File::open(TEST_FILE_NAME);
|
||||
|
||||
if input_file.is_err() {
|
||||
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
|
||||
assert!(false);
|
||||
panic!("Input file open error");
|
||||
}
|
||||
|
||||
// TODO : How to create a new BufReader using input file
|
||||
let buffered_input_file =;
|
||||
let buffered_input_file = BufReader::new(input_file.unwrap());
|
||||
|
||||
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
||||
|
||||
if output_file.is_err() {
|
||||
eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err());
|
||||
assert!(false);
|
||||
eprintln!(
|
||||
"Output file open error : {}",
|
||||
output_file.as_ref().unwrap_err()
|
||||
);
|
||||
panic!("Output file open error");
|
||||
}
|
||||
let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap());
|
||||
// TODO : How to create a new BufReader using input file
|
||||
let buffered_input_file =;
|
||||
|
||||
let mut line_number = 1;
|
||||
let mut lines = buffered_input_file.lines();
|
||||
while let Some(line) = lines.next() {
|
||||
|
||||
for line in buffered_input_file.lines() {
|
||||
if let Ok(line) = line {
|
||||
let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
|
||||
let write_result =
|
||||
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
|
||||
if write_result.is_err() {
|
||||
eprintln!("Write result error: {}", write_result.unwrap_err());
|
||||
break;
|
||||
}
|
||||
line_number += 1;
|
||||
} else {
|
||||
eprintln!("Write line error : {}", line_number);
|
||||
assert!(false);
|
||||
panic!("Write line error");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!("{} : lines processed", line_number - 1);
|
||||
@ -49,12 +48,11 @@ fn main() {
|
||||
fn create_required_files() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
|
||||
if file_path.exists() == false {
|
||||
if !file_path.exists() {
|
||||
let text = "This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.";
|
||||
fs::write(&file_path, text).unwrap();
|
||||
fs::write(file_path, text).unwrap();
|
||||
println!("File created.");
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,8 +1,7 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
|
||||
create_required_files();
|
||||
let mut path_buffer = PathBuf::new();
|
||||
|
||||
@ -18,15 +17,10 @@ fn main() {
|
||||
println!("File size {}", meta_data.len());
|
||||
assert_eq!(meta_data.len(), 117);
|
||||
println!("File permissions {:?}", meta_data.permissions());
|
||||
assert_eq!(meta_data.permissions().readonly(), false);
|
||||
assert!(!meta_data.permissions().readonly());
|
||||
} else {
|
||||
println!("Could not get metadata");
|
||||
assert!(false);
|
||||
panic!("Could not get metadata");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
@ -34,18 +28,16 @@ fn create_required_files(){
|
||||
|
||||
let dir_path = file_path.parent().unwrap();
|
||||
|
||||
if dir_path.exists() == false {
|
||||
if !dir_path.exists() {
|
||||
fs::create_dir(dir_path).unwrap();
|
||||
println!("Created directory {:?}", dir_path);
|
||||
}
|
||||
|
||||
if file_path.exists() == false {
|
||||
|
||||
if !file_path.exists() {
|
||||
let text = "This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.";
|
||||
fs::write(&file_path, text).unwrap();
|
||||
fs::write(file_path, text).unwrap();
|
||||
println!("File created.");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user