]> piware.de Git - learn-rust.git/blob - src/main.rs
Move helper functions into src/lib.rs
[learn-rust.git] / 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
8 use lib::*;
9 use word_utils::{first_word, second_word};
10
11 fn test_strings() {
12     let s = String::from("Hello world");
13     println!("first word: '{}'", first_word(&s));
14     println!("second word: '{}'", second_word(&s).unwrap());
15
16     let s2 = "hello dude blah";
17     println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)"));
18
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),
22     }
23 }
24
25 fn test_vectors() {
26     let v1 = vec![1, 2, 3];
27     println!("statically initialized vector: {:?}", v1);
28
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]);
34     for el in &mut v2 {
35         *el += "xx";
36     }
37     for el in &v2 {
38         println!("{}", el);
39     }
40 }
41
42 fn test_hashmaps() {
43     let mut scores = HashMap::new();
44     scores.insert("john", 10);
45     scores.insert("mary", 20);
46
47     println!("scores: {:?}", scores);
48
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);
53
54     for (p, s) in &collect_scores {
55         println!("person {}: score {}", p, s);
56     }
57
58     println!("john's score: {}", scores.get("john").unwrap());
59     println!("jake's score: {}", scores.get("jake").unwrap_or(&-1));
60
61     // double scores
62     for (_, v) in scores.iter_mut() {
63         *v *= 2;
64     }
65     println!("scores after doubling: {:?}", scores);
66
67     // double scores of immutable hashmap (rebuild it)
68     let collect_scores: HashMap<_, _> = collect_scores.iter()
69         .map(|(k, v)| (k, 2 * v))
70         .collect();
71     println!("collect_scores after rebuilding with doubling: {:?}", collect_scores);
72 }
73
74 fn test_files() {
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)
80         }
81     } else {
82         println!("could not open Cargo.toml");
83     }
84
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()
91         } else {
92             panic!("Could not open Cargo.toml: {:?}", e);
93         }
94     });
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);
98     });
99     println!("successfully opened Cargo.toml with unwrap_or_else: {:?}, contents {} bytes:\n{}\n----------", f, len, contents);
100
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)
105     }
106
107     // using std API
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)
111     }
112 }
113
114 fn test_generics() {
115     let num_list = vec![3, 42, -7, 100, 0];
116     println!("largest number: {}", largest(&num_list));
117     println!("num_list: {:?}", num_list);
118
119     let char_list = vec!['a', 'y', 'q', 'm'];
120     println!("largest char: {}", largest(&char_list));
121
122     let str_list = vec!["hello", "world", "blue", "planet"];
123     println!("largest str: {}", largest(&str_list));
124     println!("str_list: {:?}", str_list);
125
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);
130
131     let s1 = String::from("abcd");
132     let l;
133     {
134         let s2 = "efghi";
135         l = longest(&s1, s2);
136     }
137     println!("longest string: {}", l);
138 }
139
140 fn main() {
141     test_strings();
142     test_vectors();
143     test_hashmaps();
144     test_files();
145     test_generics();
146 }