4 use std::collections::HashMap;
5 use std::io::{prelude::*, ErrorKind};
6 use std::fs::{self, File};
9 use word_utils::{first_word, second_word};
12 let s = String::from("Hello world");
13 println!("first word: '{}'", first_word(&s));
14 println!("second word: '{}'", second_word(&s).unwrap());
16 let s2 = "hello dude blah";
17 println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)"));
19 match second_word(s2) {
20 Some(w) => println!("match: second word of '{}' exists: {}", s2, w),
21 None => println!("match: second word of '{}' does not exist", s2),
26 let v1 = vec![1, 2, 3];
27 println!("statically initialized vector: {:?}", v1);
29 let mut v2: Vec<String> = Vec::new();
30 v2.push("Hello".to_string());
31 v2.push(String::from("world"));
32 println!("dynamically built vector: {:?}", v2);
33 println!("first element: {}", v2[0]);
43 let mut scores = HashMap::new();
44 scores.insert("john", 10);
45 scores.insert("mary", 20);
47 println!("scores: {:?}", scores);
49 // hash map with .collect()
50 let persons = vec![("homer", 42), ("marge", 30)];
51 let collect_scores: HashMap<_, _> = persons.into_iter().collect();
52 println!("collect_scores: {:?}", collect_scores);
54 for (p, s) in &collect_scores {
55 println!("person {}: score {}", p, s);
58 println!("john's score: {}", scores.get("john").unwrap());
59 println!("jake's score: {}", scores.get("jake").unwrap_or(&-1));
62 for (_, v) in scores.iter_mut() {
65 println!("scores after doubling: {:?}", scores);
67 // double scores of immutable hashmap (rebuild it)
68 let collect_scores: HashMap<_, _> = collect_scores.into_iter()
69 .map(|(k, v)| (k, 2 * v))
71 println!("collect_scores after rebuilding with doubling: {:?}", collect_scores);
75 if let Ok(mut f) = File::open("Cargo.toml") {
76 let mut contents = String::new();
77 match f.read_to_string(&mut contents) {
78 Ok(len) => println!("successfully opened Cargo.toml: {:?}, contents {} bytes:\n{}\n----------", f, len, contents),
79 Err(e) => panic!("could not read file: {:?}", e)
82 println!("could not open Cargo.toml");
85 // alternative form, more specific error checking
86 let mut f = File::open("Cargo.toml").unwrap_or_else(|e| {
87 if e.kind() == ErrorKind::NotFound {
88 println!("Cargo.toml not found, falling back to /dev/null");
89 // need to return a File
90 File::open("/dev/null").unwrap()
92 panic!("Could not open Cargo.toml: {:?}", e);
95 let mut contents = String::new();
96 let len = f.read_to_string(&mut contents).unwrap_or_else(|e| {
97 panic!("Could not read file: {:?}", e);
99 println!("successfully opened Cargo.toml with unwrap_or_else: {:?}, contents {} bytes:\n{}\n----------", f, len, contents);
101 // using the '?' operator
102 match read_file("Cargo.toml") {
103 Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s),
104 Err(e) => println!("Could not open Cargo.toml: {:?}", e)
108 match fs::read_to_string("Cargo.toml") {
109 Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s),
110 Err(e) => println!("Could not open Cargo.toml: {:?}", e)
115 let num_list = vec![3, 42, -7, 100, 0];
116 println!("largest number: {}", largest(&num_list));
117 println!("num_list: {:?}", num_list);
119 let char_list = vec!['a', 'y', 'q', 'm'];
120 println!("largest char: {}", largest(&char_list));
122 let str_list = vec!["hello", "world", "blue", "planet"];
123 println!("largest str: {}", largest(&str_list));
124 println!("str_list: {:?}", str_list);
126 let string_list = vec!["aaaa".to_string(), "xxxxx".to_string(), "ffff".to_string()];
127 println!("largest string (with cloning): {}", largest_clone(&string_list));
128 println!("largest string (with ref): {}", largest_ref(&string_list));
129 println!("string_list: {:?}", string_list);
131 let s1 = String::from("abcd");
135 l = longest(&s1, s2);
137 println!("longest string: {}", l);
141 let mut expensive_int_result = Cacher::new(|x| {
142 println!("calculating expensive int result for {}", x);
146 println!("1st int call for value 1: {}", expensive_int_result.value(1));
147 println!("2nd int call for value 1: {}", expensive_int_result.value(1));
148 println!("1st int call for value 2: {}", expensive_int_result.value(2));
150 let mut expensive_str_result = Cacher::new(|x: &str| {
151 println!("calculating expensive str result for {}", x);
155 println!("1st int call for value abc: {}", expensive_str_result.value("abc"));
156 println!("2nd int call for value abc: {}", expensive_str_result.value("abc"));
157 println!("1st int call for value defg: {}", expensive_str_result.value("defg"));