From: Martin Pitt Date: Fri, 6 Jan 2023 14:47:19 +0000 (+0100) Subject: concepts: rustfmt X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=commitdiff_plain;h=refs%2Fheads%2Fmaster;ds=sidebyside concepts: rustfmt --- diff --git a/concepts/src/lib.rs b/concepts/src/lib.rs index 5f1feea..f2b1c10 100644 --- a/concepts/src/lib.rs +++ b/concepts/src/lib.rs @@ -1,11 +1,10 @@ +use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; -use std::collections::HashMap; pub fn read_file(path: &str) -> Result { let mut s = String::new(); - File::open(path)? - .read_to_string(&mut s)?; + File::open(path)?.read_to_string(&mut s)?; Ok(s) } @@ -71,7 +70,10 @@ where V: Copy, { pub fn new(calc: T) -> Cacher { - Cacher { calc, values: HashMap::new() } + Cacher { + calc, + values: HashMap::new(), + } } pub fn value(&mut self, arg: A) -> V { @@ -87,7 +89,7 @@ where } pub struct Counter5 { - count: u32 + count: u32, } impl Counter5 { @@ -157,7 +159,7 @@ trait State { struct Draft {} impl State for Draft { fn request_review(&self) -> Box { - Box::new(PendingReview {acks: 0}) + Box::new(PendingReview { acks: 0 }) } fn approve(&mut self) -> Box { @@ -176,14 +178,16 @@ struct PendingReview { impl State for PendingReview { fn request_review(&self) -> Box { - Box::new(Self {acks: self.acks}) + Box::new(Self { acks: self.acks }) } fn approve(&mut self) -> Box { if self.acks >= 1 { Box::new(Published {}) } else { - Box::new(Self {acks: self.acks + 1}) + Box::new(Self { + acks: self.acks + 1, + }) } } @@ -218,7 +222,9 @@ pub struct TPost { impl TPost { pub fn new() -> TPostDraft { - TPostDraft {content: String::new()} + TPostDraft { + content: String::new(), + } } pub fn content(&self) -> &str { @@ -236,7 +242,9 @@ impl TPostDraft { } pub fn request_review(self) -> TPostReview { - TPostReview {content: self.content} + TPostReview { + content: self.content, + } } } @@ -246,10 +254,14 @@ pub struct TPostReview { impl TPostReview { pub fn approve(self) -> TPost { - TPost {content: self.content} + TPost { + content: self.content, + } } pub fn reject(self) -> TPostDraft { - TPostDraft {content: self.content} + TPostDraft { + content: self.content, + } } } 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()); }