X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=src%2Fmain.rs;h=441cc79a44db49e65227afdacfa575de42e96fb9;hp=7a83cb487326028805912df69daa38a3c2cfa394;hb=80e4d9aff6de0cdeaedc8db269a18c6da91d0239;hpb=da5e8d881fe6e659d168b7ace5ab9af35541a613 diff --git a/src/main.rs b/src/main.rs index 7a83cb4..441cc79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,12 @@ -mod word_utils { - pub fn first_word(s: &str) -> &str { - for (i, &item) in s.as_bytes().iter().enumerate() { - if item == b' ' { - return &s[..i]; - } - } +mod word_utils; - s - } - - pub fn second_word(s: &str) -> Option<&str> { - for (i, &item) in s.as_bytes().iter().enumerate() { - if item == b' ' { - return Some(first_word(&s[(i + 1)..])); - } - } - - return None; - } -} +use std::collections::HashMap; +use std::io::{prelude::*, ErrorKind}; +use std::fs::File; use word_utils::{first_word, second_word}; -fn main() { +fn test_strings() { let s = String::from("Hello world"); println!("first word: '{}'", first_word(&s)); println!("second word: '{}'", second_word(&s).unwrap()); @@ -35,3 +19,100 @@ fn main() { None => println!("match: second word of '{}' does not exist", s2), } } + +fn test_vectors() { + let v1 = vec![1, 2, 3]; + println!("statically initialized vector: {:?}", v1); + + let mut v2: Vec = Vec::new(); + v2.push("Hello".to_string()); + v2.push(String::from("world")); + println!("dynamically built vector: {:?}", v2); + println!("first element: {}", v2[0]); + for el in &mut v2 { + *el += "xx"; + } + for el in &v2 { + println!("{}", el); + } +} + +fn test_hashmaps() { + let mut scores = HashMap::new(); + scores.insert("john", 10); + scores.insert("mary", 20); + + println!("scores: {:?}", scores); + + // hash map with .collect() + let persons = vec![("homer", 42), ("marge", 30)]; + let collect_scores: HashMap<_, _> = persons.into_iter().collect(); + println!("collect_scores: {:?}", collect_scores); + + for (p, s) in &collect_scores { + println!("person {}: score {}", p, s); + } + + println!("john's score: {}", scores.get("john").unwrap()); + println!("jake's score: {}", scores.get("jake").unwrap_or(&-1)); + + // double scores + for (_, v) in scores.iter_mut() { + *v *= 2; + } + println!("scores after doubling: {:?}", scores); + + // double scores of immutable hashmap (rebuild it) + let collect_scores: HashMap<_, _> = collect_scores.iter() + .map(|(k, v)| (k, 2 * v)) + .collect(); + println!("collect_scores after rebuilding with doubling: {:?}", collect_scores); +} + +fn read_file(path: &str) -> Result { + let mut s = String::new(); + File::open(path)? + .read_to_string(&mut s)?; + Ok(s) +} + +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) + } + } else { + println!("could not open Cargo.toml"); + } + + // alternative form, more specific error checking + let mut f = File::open("Cargo.toml").unwrap_or_else(|e| { + if e.kind() == ErrorKind::NotFound { + println!("Cargo.toml not found, falling back to /dev/null"); + // need to return a File + File::open("/dev/null").unwrap() + } else { + panic!("Could not open Cargo.toml: {:?}", e); + } + }); + let mut contents = String::new(); + let len = f.read_to_string(&mut contents).unwrap_or_else(|e| { + panic!("Could not read file: {:?}", e); + }); + println!("successfully opened Cargo.toml with unwrap_or_else: {:?}, contents {} bytes:\n{}\n----------", f, len, contents); + + // 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) + } +} + +fn main() { + test_strings(); + test_vectors(); + test_hashmaps(); + test_files(); +}