4 use std::collections::HashMap;
5 use std::io::{prelude::*, ErrorKind};
6 use std::fs::{self, File};
7 use std::{thread, time, sync};
10 use word_utils::{first_word, second_word};
13 let s = String::from("Hello world");
14 println!("first word: '{}'", first_word(&s));
15 println!("second word: '{}'", second_word(&s).unwrap());
17 let s2 = "hello dude blah";
18 println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)"));
20 match second_word(s2) {
21 Some(w) => println!("match: second word of '{}' exists: {}", s2, w),
22 None => println!("match: second word of '{}' does not exist", s2),
27 let v1 = vec![1, 2, 3];
28 println!("statically initialized vector: {:?}", v1);
30 let mut v2: Vec<String> = Vec::new();
31 v2.push("Hello".to_string());
32 v2.push(String::from("world"));
33 println!("dynamically built vector: {:?}", v2);
34 println!("first element: {}", v2[0]);
44 let mut scores = HashMap::new();
45 scores.insert("john", 10);
46 scores.insert("mary", 20);
48 println!("scores: {:?}", scores);
50 // hash map with .collect()
51 let persons = vec![("homer", 42), ("marge", 30)];
52 let collect_scores: HashMap<_, _> = persons.into_iter().collect();
53 println!("collect_scores: {:?}", collect_scores);
55 for (p, s) in &collect_scores {
56 println!("person {}: score {}", p, s);
59 println!("john's score: {}", scores.get("john").unwrap());
60 println!("jake's score: {}", scores.get("jake").unwrap_or(&-1));
63 for (_, v) in scores.iter_mut() {
66 println!("scores after doubling: {:?}", scores);
68 // double scores of immutable hashmap (rebuild it)
69 let collect_scores: HashMap<_, _> = collect_scores.into_iter()
70 .map(|(k, v)| (k, 2 * v))
72 println!("collect_scores after rebuilding with doubling: {:?}", collect_scores);
76 if let Ok(mut f) = File::open("Cargo.toml") {
77 let mut contents = String::new();
78 match f.read_to_string(&mut contents) {
79 Ok(len) => println!("successfully opened Cargo.toml: {:?}, contents {} bytes:\n{}\n----------", f, len, contents),
80 Err(e) => panic!("could not read file: {:?}", e)
83 println!("could not open Cargo.toml");
86 // alternative form, more specific error checking
87 let mut f = File::open("Cargo.toml").unwrap_or_else(|e| {
88 if e.kind() == ErrorKind::NotFound {
89 println!("Cargo.toml not found, falling back to /dev/null");
90 // need to return a File
91 File::open("/dev/null").unwrap()
93 panic!("Could not open Cargo.toml: {:?}", e);
96 let mut contents = String::new();
97 let len = f.read_to_string(&mut contents).unwrap_or_else(|e| {
98 panic!("Could not read file: {:?}", e);
100 println!("successfully opened Cargo.toml with unwrap_or_else: {:?}, contents {} bytes:\n{}\n----------", f, len, contents);
102 // using the '?' operator
103 match read_file("Cargo.toml") {
104 Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s),
105 Err(e) => println!("Could not open Cargo.toml: {:?}", e)
109 match fs::read_to_string("Cargo.toml") {
110 Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s),
111 Err(e) => println!("Could not open Cargo.toml: {:?}", e)
116 let num_list = vec![3, 42, -7, 100, 0];
117 println!("largest number: {}", largest(&num_list));
118 println!("num_list: {:?}", num_list);
120 let char_list = vec!['a', 'y', 'q', 'm'];
121 println!("largest char: {}", largest(&char_list));
123 let str_list = vec!["hello", "world", "blue", "planet"];
124 println!("largest str: {}", largest(&str_list));
125 println!("str_list: {:?}", str_list);
127 let string_list = vec!["aaaa".to_string(), "xxxxx".to_string(), "ffff".to_string()];
128 println!("largest string (with cloning): {}", largest_clone(&string_list));
129 println!("largest string (with ref): {}", largest_ref(&string_list));
130 println!("string_list: {:?}", string_list);
132 let s1 = String::from("abcd");
136 l = longest(&s1, s2);
138 println!("longest string: {}", l);
142 let mut expensive_int_result = Cacher::new(|x| {
143 println!("calculating expensive int result for {}", x);
147 println!("1st int call for value 1: {}", expensive_int_result.value(1));
148 println!("2nd int call for value 1: {}", expensive_int_result.value(1));
149 println!("1st int call for value 2: {}", expensive_int_result.value(2));
151 let mut expensive_str_result = Cacher::new(|x: &str| {
152 println!("calculating expensive str result for {}", x);
156 println!("1st int call for value abc: {}", expensive_str_result.value("abc"));
157 println!("2nd int call for value abc: {}", expensive_str_result.value("abc"));
158 println!("1st int call for value defg: {}", expensive_str_result.value("defg"));
161 fn test_iterators() {
162 let v1 = vec!["Hello", "good", "world"];
163 // implied default is .into_iter() which consumes v1
165 println!("element: {}", s);
167 // can still use it now
168 println!("v1: {:?}", &v1);
171 let mut v1_iter = v1.iter();
172 while let Some(e) = v1_iter.next() {
173 println!("while loop over iter: {}", e);
176 for l_element in v1.iter().filter(|x| x.contains('l')) {
177 println!("v1 element containing 'l': {}", l_element);
180 let v2 = vec![1, 2, 3];
181 let double_v2: Vec<_> = v2.iter().map(|x| x * 2).collect();
182 println!("doubled v2: {:?}", double_v2);
184 for i in v2.into_iter().map(|x| x * 3) {
185 println!("for loop triplicating v2: {}", i);
188 for i in Counter5::new() {
189 println!("Counter 5 value: {}", i);
194 let t1 = thread::spawn(|| {
196 println!("hello #{} from thread", i);
197 thread::sleep(time::Duration::from_millis(1));
202 println!("hello #{} from main", i);
203 thread::sleep(time::Duration::from_millis(1));
209 let (tx1, rx) = sync::mpsc::channel();
210 let tx2 = tx1.clone();
212 let sender1 = thread::spawn(move || {
213 for s in ["hey", "from", "sender", "one"] {
214 tx1.send(s).expect("failed to send");
215 thread::sleep(time::Duration::from_millis(100));
219 let sender2 = thread::spawn(move || {
220 for s in ["Servus", "von", "Produzent", "zwei"] {
221 tx2.send(s).expect("failed to send");
222 thread::sleep(time::Duration::from_millis(100));
227 println!("received {} from mpsc", received);
230 sender1.join().unwrap();
231 sender2.join().unwrap();
234 let counter = sync::Arc::new(sync::Mutex::new(0));
235 let mut threads = vec![];
237 let tc = sync::Arc::clone(&counter);
238 threads.push(thread::spawn(move || {
239 *tc.lock().unwrap() += 1;
247 println!("counter: {}", *counter.lock().unwrap());
250 fn test_dyn_traits() {
251 let text = "I ate a salad for lunch today";
252 let mut post = Post::new();
254 assert_eq!("", post.content());
256 post.request_review();
257 assert_eq!("", post.content());
260 assert_eq!("", post.content());
262 post.request_review();
263 assert_eq!("", post.content());
266 assert_eq!(text, post.content());