Skip to content

Commit d8155b6

Browse files
committed
Reformatted all code
1 parent 09841c1 commit d8155b6

File tree

18 files changed

+122
-143
lines changed

18 files changed

+122
-143
lines changed

Diff for: Readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Videos: https://www.youtube.com/@rust-academy
99
5. [Structs](structs)
1010
6. [Enums and pattern matching](enums)
1111
7. [Packages, crates, and modules](modules)
12-
8. [Common collections](collections)
12+
8. [Common collections](collections)
1313
9. [Error handling](errors)
1414
10. Generics, Traits, and Lifetimes
1515
* [Generics](generics)
1616
* [Traits](traits)
1717
* [Lifetimes](lifetimes)
18-
18+
1919
11.[Testing](testing)

Diff for: cli/src/config.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub struct Config {
99

1010
impl Config {
1111
pub fn new(args: &[String]) -> Result<Config, &'static str> {
12-
1312
if args.len() < 3 {
1413
return Err("not enough arguments. use: -- word file.txt ");
1514
}
@@ -29,16 +28,15 @@ impl Config {
2928

3029
/// build is a more comprehensive constructor that checks each argument individually
3130
pub fn build(mut args: impl Iterator<Item=String>) -> Result<Config, &'static str> {
32-
3331
args.next(); // skip first argument
3432

3533
// iterating through each argument allows specific error messages
36-
let query = match args.next(){
34+
let query = match args.next() {
3735
None => return Err("Didn't get a query string"),
3836
Some(arg) => arg,
3937
};
4038

41-
let file_path = match args.next(){
39+
let file_path = match args.next() {
4240
None => return Err("Didn't get a file path"),
4341
Some(arg) => arg,
4442
};
@@ -54,18 +52,17 @@ impl Config {
5452
file_path,
5553
case_sensitive,
5654
})
57-
5855
}
5956
}
6057

61-
impl Config{
62-
pub fn query(&self) -> String{
58+
impl Config {
59+
pub fn query(&self) -> String {
6360
self.query.clone()
6461
}
6562

66-
pub fn file_path(&self) -> String{
63+
pub fn file_path(&self) -> String {
6764
self.file_path.clone()
68-
}
65+
}
6966

7067
pub fn case_sensitive(&self) -> bool {
7168
self.case_sensitive

Diff for: cli/src/main.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use std::{env, process};
2+
3+
use crate::search::search_word_in_file;
4+
use crate::utils::get_config;
5+
16
mod simple;
27
mod config;
38
mod utils;
49
mod search;
510

6-
use std::{env, process};
7-
use crate::search::search_word_in_file;
8-
use crate::utils::get_config;
9-
1011
// Requirements
1112
// 1) Parse two command line argument, the word to search and the file path
1213
// 2) Read the file from the path into a string
@@ -15,7 +16,7 @@ use crate::utils::get_config;
1516
fn main() {
1617
let args: Vec<String> = env::args().collect();
1718
let config = get_config(args);
18-
if let Err(e) = search_word_in_file(config){
19+
if let Err(e) = search_word_in_file(config) {
1920
eprintln!("Application error: {e}"); // redirects output to stdout i.e. terminal
2021
process::exit(1);
2122
}

Diff for: cli/src/search.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
use std::error::Error;
22
use std::fs;
3+
34
use crate::config::Config;
45

56
pub fn search_word_in_file(config: Config) -> Result<(), Box<dyn Error>> {
6-
77
let path = config.file_path();
88
let pat = &config.query();
99
let content = &fs::read_to_string(path)
1010
.expect("Error opening file");
1111

12-
let result = if config.case_sensitive(){
12+
let result = if config.case_sensitive() {
1313
search_case_sensitive(pat, content)
14-
} else{
14+
} else {
1515
search_case_insensitive(pat, content)
1616
};
1717

1818
if result.len() > 0 {
19-
for line in result{
19+
for line in result {
2020
println!("{}", line);
2121
}
22-
23-
} else{
22+
} else {
2423
println!("No results found");
2524
}
2625

2726
Ok(())
2827
}
2928

3029

31-
fn search_case_sensitive<'a>(pat: &'a str, content: &'a str) -> Vec<&'a str>{
30+
fn search_case_sensitive<'a>(pat: &'a str, content: &'a str) -> Vec<&'a str> {
3231
content.lines().filter(|line| line.contains(&pat)).collect()
3332
}
3433

@@ -67,8 +66,5 @@ Trust me.";
6766
expected,
6867
search_case_insensitive(query, contents)
6968
);
70-
7169
}
72-
73-
7470
}

Diff for: cli/src/simple.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
2-
31
// _simple_word_search contains a number of problems, among others:
42
// 1. It does not check if the arguments, query & path actually exist.
53
// 2. It does not check if the file can actually be opened and read.
64
// 3. It does not check if the search result contains any values.
75
use std::fs;
86

9-
pub fn _simple_word_search(args: Vec<String>){
7+
pub fn _simple_word_search(args: Vec<String>) {
108
// 1) Parse two command line argument, the word to search and the file path
119
let word = args[1].to_string();
1210
let path = args[2].to_string();
@@ -15,7 +13,7 @@ pub fn _simple_word_search(args: Vec<String>){
1513
let content = &fs::read_to_string(&path).unwrap();
1614

1715
// 3) Search the word in text from the file
18-
let result: Vec<&str> = content.lines().filter(| line| line.contains(&word)).collect();
16+
let result: Vec<&str> = content.lines().filter(|line| line.contains(&word)).collect();
1917

2018
// 4) Print out each line where the word occurs
2119
for line in result {

Diff for: cli/src/utils.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ pub fn get_config(args: Vec<String>) -> Config {
66
let config: Config;
77

88
if args.len() > 0 {
9-
config = Config::new(&args).unwrap_or_else(|err|{
9+
config = Config::new(&args).unwrap_or_else(|err| {
1010
eprintln!("Problem parsing arguments: {}", err);
1111
process::exit(1);
1212
}
1313
);
14-
} else{
15-
config = Config::build(env::args()).unwrap_or_else( |err |{
14+
} else {
15+
config = Config::build(env::args()).unwrap_or_else(|err| {
1616
eprintln!("Problem parsing arguments: {}", err);
1717
process::exit(1);
1818
}
@@ -35,6 +35,5 @@ mod tests {
3535
assert_eq!(expected.query(), result.query());
3636
assert_eq!(expected.file_path(), result.file_path());
3737
assert_eq!(expected.case_sensitive(), result.case_sensitive());
38-
3938
}
4039
}

Diff for: errors/src/errors.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::fs::File;
21
use std::{fs, io};
2+
use std::fs::File;
33
use std::io::Read;
44

55
pub fn open_file() {
@@ -12,10 +12,9 @@ pub fn open_file() {
1212
pub fn result_match_panic(path: &str) {
1313
let file = File::open("Hello.txt");
1414
match file {
15-
Ok(file) => {},
15+
Ok(file) => {}
1616
Err(e) => panic!("Failed to open {} Error: {}", path, e),
1717
};
18-
1918
}
2019

2120
/// recoverable_file_open does the following
@@ -31,29 +30,29 @@ pub fn result_match_panic(path: &str) {
3130
pub fn recoverable_file_open(path: &str) {
3231
let file = File::open("Hello.txt");
3332
match file {
34-
Ok(file) => {},
33+
Ok(file) => {}
3534
Err(e) =>
36-
match e.kind() {
37-
io::ErrorKind::NotFound => {
38-
match File::create(path) {
39-
Ok(file) => (), // return the file
40-
Err(e) =>{
41-
println!("Error creating file: {}", e);
42-
return;
43-
}
44-
}
45-
},
46-
other_error => {
47-
panic!("Problem opening the file: {:?}", other_error)
35+
match e.kind() {
36+
io::ErrorKind::NotFound => {
37+
match File::create(path) {
38+
Ok(file) => (), // return the file
39+
Err(e) => {
40+
println!("Error creating file: {}", e);
41+
return;
42+
}
4843
}
49-
},
44+
}
45+
other_error => {
46+
panic!("Problem opening the file: {:?}", other_error)
47+
}
48+
},
5049
};
5150
}
5251

5352
// Type alias for return type
5453
pub type StringResult = Result<String, io::Error>;
5554

56-
pub fn read_username_from_file(path: &str) -> StringResult{
55+
pub fn read_username_from_file(path: &str) -> StringResult {
5756
let file = File::open(path);
5857

5958
let mut f = match file {

Diff for: errors/src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const UNRECOVERABLE: bool = false;
66
fn main() {
77
if UNRECOVERABLE {
88
unrecoverable();
9-
} else{
9+
} else {
1010
recoverable();
1111
}
1212
}
1313

14-
fn unrecoverable(){
14+
fn unrecoverable() {
1515
// test_panic();
1616
// errors::open_file();
1717

@@ -24,7 +24,7 @@ fn test_panic() {
2424
}
2525

2626

27-
fn recoverable(){
27+
fn recoverable() {
2828
let path = "Hello.txt";
2929
errors::recoverable_file_open(path);
3030
}

Diff for: functional/src/closures.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ fn simple_closure() {
1313

1414
let _x = 5;
1515

16-
let expensive_closure = |num:i32| {
16+
let expensive_closure = |num: i32| {
1717
println!("calculating slowly...");
1818
thread::sleep(Duration::from_micros(num as u64));
19-
num +21
19+
num + 21
2020
};
2121

2222
let forty_two = expensive_closure(21);
@@ -35,11 +35,12 @@ fn closure_syntax() {
3535
// * Semicolon required ;-)
3636

3737
// Most simple_closure syntax
38-
let add_one = |num: i32| num+1;
38+
let add_one = |num: i32| num + 1;
3939
println!("{}", add_one(0));
4040

4141
// Overblown syntax with all clutter
42-
fn add_one_v1(x: u32) -> u32 { x + 1 };
42+
fn add_one_v1(x: u32) -> u32 { x + 1 }
43+
;
4344

4445
println!("{}", add_one_v1(1));
4546

@@ -49,7 +50,7 @@ fn closure_syntax() {
4950
println!("{}", add_one_v2(2));
5051

5152
// remove all type annotation
52-
let add_one_v3 = |x| { x + 1 };
53+
let add_one_v3 = |x| { x + 1 };
5354
println!("{:?}", add_one_v3(3)); // println falls back to debug b/c of missing type inference
5455

5556
// Removed curly brackets.
@@ -68,14 +69,13 @@ fn closure_syntax() {
6869

6970
let no_arg = || println!("Nothing captured");
7071
no_arg();
71-
7272
}
7373

7474
fn closure_context_capture() {
7575
// Closures capture values from their local context in one of three ways:
7676

7777
// 1) borrowing immutably.
78-
immutable_borrow_closure();
78+
immutable_borrow_closure();
7979

8080
// 2) borrowing mutably.
8181
mutable_borrow_closure();
@@ -84,7 +84,7 @@ fn closure_context_capture() {
8484
taking_ownership_closure();
8585
}
8686

87-
fn immutable_borrow_closure(){
87+
fn immutable_borrow_closure() {
8888
println!("immutable_borrow_closure");
8989

9090
// collection with sample data
@@ -97,7 +97,7 @@ fn immutable_borrow_closure(){
9797
println!("After calling closure: {:?}", list);
9898
}
9999

100-
fn mutable_borrow_closure(){
100+
fn mutable_borrow_closure() {
101101
println!("mutable_borrow_closure");
102102
// mutable collection with sample data
103103
let mut list = vec![1, 2, 3];
@@ -111,7 +111,7 @@ fn mutable_borrow_closure(){
111111
}
112112

113113

114-
fn taking_ownership_closure(){
114+
fn taking_ownership_closure() {
115115
println!("taking_ownership_closure");
116116

117117
// If you want to force the closure to take ownership of the values it uses in the environment
@@ -126,15 +126,14 @@ fn taking_ownership_closure(){
126126
thread::spawn(move || closure)
127127
.join()
128128
.unwrap();
129-
130129
}
131130

132131

133132
fn closure_context_value() {
134133
// A closure body can do any of the following to the value captured fro its context:
135134

136135
// 1) move a captured value out of the closure,
137-
move_captured_value_closure();
136+
move_captured_value_closure();
138137

139138
// 2) mutate the captured value,
140139
mutate_captured_value_closure();
@@ -148,7 +147,8 @@ fn closure_context_value() {
148147

149148
#[derive(Debug)]
150149
struct Rectangle {
151-
width: u32, height: u32,
150+
width: u32,
151+
height: u32,
152152
}
153153

154154
fn move_captured_value_closure() {
@@ -162,7 +162,10 @@ fn move_captured_value_closure() {
162162

163163
let mut num_sort_operations = 0;
164164

165-
list.sort_by_key(|r| {num_sort_operations+=1; r.width});
165+
list.sort_by_key(|r| {
166+
num_sort_operations += 1;
167+
r.width
168+
});
166169

167170
println!("{:#?}, sorted in {num_sort_operations} operations", list)
168171
}

0 commit comments

Comments
 (0)