+use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
-use std::collections::HashMap;
pub fn read_file(path: &str) -> Result<String, std::io::Error> {
let mut s = String::new();
- File::open(path)?
- .read_to_string(&mut s)?;
+ File::open(path)?.read_to_string(&mut s)?;
Ok(s)
}
V: Copy,
{
pub fn new(calc: T) -> Cacher<T, A, V> {
- Cacher { calc, values: HashMap::new() }
+ Cacher {
+ calc,
+ values: HashMap::new(),
+ }
}
pub fn value(&mut self, arg: A) -> V {
}
pub struct Counter5 {
- count: u32
+ count: u32,
}
impl Counter5 {
struct Draft {}
impl State for Draft {
fn request_review(&self) -> Box<dyn State> {
- Box::new(PendingReview {acks: 0})
+ Box::new(PendingReview { acks: 0 })
}
fn approve(&mut self) -> Box<dyn State> {
impl State for PendingReview {
fn request_review(&self) -> Box<dyn State> {
- Box::new(Self {acks: self.acks})
+ Box::new(Self { acks: self.acks })
}
fn approve(&mut self) -> Box<dyn State> {
if self.acks >= 1 {
Box::new(Published {})
} else {
- Box::new(Self {acks: self.acks + 1})
+ Box::new(Self {
+ acks: self.acks + 1,
+ })
}
}
impl TPost {
pub fn new() -> TPostDraft {
- TPostDraft {content: String::new()}
+ TPostDraft {
+ content: String::new(),
+ }
}
pub fn content(&self) -> &str {
}
pub fn request_review(self) -> TPostReview {
- TPostReview {content: self.content}
+ TPostReview {
+ content: self.content,
+ }
}
}
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,
+ }
}
}
-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};
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),
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");
// 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),
}
}
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);
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() {
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());
}