X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=concepts%2Fsrc%2Fmain.rs;fp=concepts%2Fsrc%2Fmain.rs;h=be26bdb6f2f8bb4212291f5aeb857dcf6bd4e6a8;hp=4b6df6447be9930e5773202f24601cd817722eb8;hb=1354104c119bdcf6e8050c1a26a53bc77dbb85a6;hpb=121e3e9e83ec2bba3f0ebd22bdb1128c83fd5321 diff --git a/concepts/src/main.rs b/concepts/src/main.rs index 4b6df64..be26bdb 100644 --- a/concepts/src/main.rs +++ b/concepts/src/main.rs @@ -1,10 +1,10 @@ -mod word_utils; mod lib; +mod word_utils; use std::collections::HashMap; -use std::io::{prelude::*, ErrorKind}; use std::fs::{self, File}; -use std::{thread, time, sync}; +use std::io::{prelude::*, ErrorKind}; +use std::{sync, thread, time}; use lib::*; use word_utils::{first_word, second_word}; @@ -15,7 +15,10 @@ fn test_strings() { println!("second word: '{}'", second_word(&s).unwrap()); let s2 = "hello dude blah"; - println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)")); + println!( + "second word of single: '{}'", + second_word(s2).unwrap_or("(none)") + ); match second_word(s2) { Some(w) => println!("match: second word of '{}' exists: {}", s2, w), @@ -66,18 +69,25 @@ fn test_hashmaps() { println!("scores after doubling: {:?}", scores); // double scores of immutable hashmap (rebuild it) - let collect_scores: HashMap<_, _> = collect_scores.into_iter() + let collect_scores: HashMap<_, _> = collect_scores + .into_iter() .map(|(k, v)| (k, 2 * v)) .collect(); - println!("collect_scores after rebuilding with doubling: {:?}", collect_scores); + println!( + "collect_scores after rebuilding with doubling: {:?}", + collect_scores + ); } fn test_files() { if let Ok(mut f) = File::open("Cargo.toml") { let mut contents = String::new(); match f.read_to_string(&mut contents) { - Ok(len) => println!("successfully opened Cargo.toml: {:?}, contents {} bytes:\n{}\n----------", f, len, contents), - Err(e) => panic!("could not read file: {:?}", e) + Ok(len) => println!( + "successfully opened Cargo.toml: {:?}, contents {} bytes:\n{}\n----------", + f, len, contents + ), + Err(e) => panic!("could not read file: {:?}", e), } } else { println!("could not open Cargo.toml"); @@ -102,13 +112,13 @@ fn test_files() { // using the '?' operator match read_file("Cargo.toml") { Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s), - Err(e) => println!("Could not open Cargo.toml: {:?}", e) + Err(e) => println!("Could not open Cargo.toml: {:?}", e), } // using std API match fs::read_to_string("Cargo.toml") { Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s), - Err(e) => println!("Could not open Cargo.toml: {:?}", e) + Err(e) => println!("Could not open Cargo.toml: {:?}", e), } } @@ -125,7 +135,10 @@ fn test_generics() { println!("str_list: {:?}", str_list); let string_list = vec!["aaaa".to_string(), "xxxxx".to_string(), "ffff".to_string()]; - println!("largest string (with cloning): {}", largest_clone(&string_list)); + println!( + "largest string (with cloning): {}", + largest_clone(&string_list) + ); println!("largest string (with ref): {}", largest_ref(&string_list)); println!("string_list: {:?}", string_list); @@ -144,18 +157,36 @@ fn test_closures() { 2 * x }); - println!("1st int call for value 1: {}", expensive_int_result.value(1)); - println!("2nd int call for value 1: {}", expensive_int_result.value(1)); - println!("1st int call for value 2: {}", expensive_int_result.value(2)); + println!( + "1st int call for value 1: {}", + expensive_int_result.value(1) + ); + println!( + "2nd int call for value 1: {}", + expensive_int_result.value(1) + ); + println!( + "1st int call for value 2: {}", + expensive_int_result.value(2) + ); let mut expensive_str_result = Cacher::new(|x: &str| { println!("calculating expensive str result for {}", x); x.len() }); - println!("1st int call for value abc: {}", expensive_str_result.value("abc")); - println!("2nd int call for value abc: {}", expensive_str_result.value("abc")); - println!("1st int call for value defg: {}", expensive_str_result.value("defg")); + println!( + "1st int call for value abc: {}", + expensive_str_result.value("abc") + ); + println!( + "2nd int call for value abc: {}", + expensive_str_result.value("abc") + ); + println!( + "1st int call for value defg: {}", + expensive_str_result.value("defg") + ); } fn test_iterators() { @@ -262,13 +293,13 @@ fn test_dyn_traits() { post.request_review(); assert_eq!("", post.content()); - post.approve(); // first + post.approve(); // first assert_eq!("", post.content()); - post.approve(); // second + post.approve(); // second assert_eq!(text, post.content()); - post.reject(); // no-op + post.reject(); // no-op assert_eq!(text, post.content()); }