]> piware.de Git - learn-rust.git/blob - concepts/src/main.rs
3e9bbbd0cd042234615d20dd563baf6278258464
[learn-rust.git] / concepts / src / main.rs
1 mod word_utils;
2 mod lib;
3
4 use std::collections::HashMap;
5 use std::io::{prelude::*, ErrorKind};
6 use std::fs::{self, File};
7 use std::{thread, time, sync};
8
9 use lib::*;
10 use word_utils::{first_word, second_word};
11
12 fn test_strings() {
13     let s = String::from("Hello world");
14     println!("first word: '{}'", first_word(&s));
15     println!("second word: '{}'", second_word(&s).unwrap());
16
17     let s2 = "hello dude blah";
18     println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)"));
19
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),
23     }
24 }
25
26 fn test_vectors() {
27     let v1 = vec![1, 2, 3];
28     println!("statically initialized vector: {:?}", v1);
29
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]);
35     for el in &mut v2 {
36         *el += "xx";
37     }
38     for el in &v2 {
39         println!("{}", el);
40     }
41 }
42
43 fn test_hashmaps() {
44     let mut scores = HashMap::new();
45     scores.insert("john", 10);
46     scores.insert("mary", 20);
47
48     println!("scores: {:?}", scores);
49
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);
54
55     for (p, s) in &collect_scores {
56         println!("person {}: score {}", p, s);
57     }
58
59     println!("john's score: {}", scores.get("john").unwrap());
60     println!("jake's score: {}", scores.get("jake").unwrap_or(&-1));
61
62     // double scores
63     for (_, v) in scores.iter_mut() {
64         *v *= 2;
65     }
66     println!("scores after doubling: {:?}", scores);
67
68     // double scores of immutable hashmap (rebuild it)
69     let collect_scores: HashMap<_, _> = collect_scores.into_iter()
70         .map(|(k, v)| (k, 2 * v))
71         .collect();
72     println!("collect_scores after rebuilding with doubling: {:?}", collect_scores);
73 }
74
75 fn test_files() {
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)
81         }
82     } else {
83         println!("could not open Cargo.toml");
84     }
85
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()
92         } else {
93             panic!("Could not open Cargo.toml: {:?}", e);
94         }
95     });
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);
99     });
100     println!("successfully opened Cargo.toml with unwrap_or_else: {:?}, contents {} bytes:\n{}\n----------", f, len, contents);
101
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)
106     }
107
108     // using std API
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)
112     }
113 }
114
115 fn test_generics() {
116     let num_list = vec![3, 42, -7, 100, 0];
117     println!("largest number: {}", largest(&num_list));
118     println!("num_list: {:?}", num_list);
119
120     let char_list = vec!['a', 'y', 'q', 'm'];
121     println!("largest char: {}", largest(&char_list));
122
123     let str_list = vec!["hello", "world", "blue", "planet"];
124     println!("largest str: {}", largest(&str_list));
125     println!("str_list: {:?}", str_list);
126
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);
131
132     let s1 = String::from("abcd");
133     let l;
134     {
135         let s2 = "efghi";
136         l = longest(&s1, s2);
137     }
138     println!("longest string: {}", l);
139 }
140
141 fn test_closures() {
142     let mut expensive_int_result = Cacher::new(|x| {
143         println!("calculating expensive int result for {}", x);
144         2 * x
145     });
146
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));
150
151     let mut expensive_str_result = Cacher::new(|x: &str| {
152         println!("calculating expensive str result for {}", x);
153         x.len()
154     });
155
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"));
159 }
160
161 fn test_iterators() {
162     let v1 = vec!["Hello", "good", "world"];
163     // implied default is .into_iter() which consumes v1
164     for s in v1.iter() {
165         println!("element: {}", s);
166     }
167     // can still use it now
168     println!("v1: {:?}", &v1);
169
170     // manual iteration
171     let mut v1_iter = v1.iter();
172     while let Some(e) = v1_iter.next() {
173         println!("while loop over iter: {}", e);
174     }
175
176     for l_element in v1.iter().filter(|x| x.contains('l')) {
177         println!("v1 element containing 'l': {}", l_element);
178     }
179
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);
183
184     for i in v2.into_iter().map(|x| x * 3) {
185         println!("for loop triplicating v2: {}", i);
186     }
187
188     for i in Counter5::new() {
189         println!("Counter 5 value: {}", i);
190     }
191 }
192
193 fn test_threads() {
194     let t1 = thread::spawn(|| {
195         for i in 0..10 {
196             println!("hello #{} from thread", i);
197             thread::sleep(time::Duration::from_millis(1));
198         }
199     });
200
201     for i in 1..5 {
202         println!("hello #{} from main", i);
203         thread::sleep(time::Duration::from_millis(1));
204     }
205
206     t1.join().unwrap();
207
208     // message passing
209     let (tx1, rx) = sync::mpsc::channel();
210     let tx2 = tx1.clone();
211
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));
216         }
217     });
218
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));
223         }
224     });
225
226     for received in rx {
227         println!("received {} from mpsc", received);
228     }
229
230     sender1.join().unwrap();
231     sender2.join().unwrap();
232
233     // shared state
234     let counter = sync::Arc::new(sync::Mutex::new(0));
235     let mut threads = vec![];
236     for _ in 0..10 {
237         let tc = sync::Arc::clone(&counter);
238         threads.push(thread::spawn(move || {
239             *tc.lock().unwrap() += 1;
240         }));
241     }
242
243     for t in threads {
244         t.join().unwrap();
245     }
246
247     println!("counter: {}", *counter.lock().unwrap());
248 }
249
250 fn test_dyn_traits() {
251     let text = "I ate a salad for lunch today";
252     let mut post = Post::new();
253     post.add_text(text);
254     assert_eq!("", post.content());
255
256     post.request_review();
257     assert_eq!("", post.content());
258
259     post.reject();
260     assert_eq!("", post.content());
261
262     post.request_review();
263     assert_eq!("", post.content());
264
265     post.approve();
266     assert_eq!(text, post.content());
267 }
268
269 fn main() {
270     test_strings();
271     test_vectors();
272     test_hashmaps();
273     test_files();
274     test_generics();
275     test_closures();
276     test_iterators();
277     test_threads();
278     test_dyn_traits();
279 }