X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=src%2Fmain.rs;h=718ecb5f22028762b437c4b87a60dac13f99c90c;hp=973bb2e6c8a3ff790ed25a03f690ea48c6a29084;hb=f026aa1e762803c4e784f82fd3830c9f936cf8d5;hpb=56e1367f9394b43c2c8f1e4119df70f02d57ba75 diff --git a/src/main.rs b/src/main.rs index 973bb2e..718ecb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ mod word_utils; +mod lib; use std::collections::HashMap; use std::io::{prelude::*, ErrorKind}; -use std::fs::File; +use std::fs::{self, File}; +use lib::*; use word_utils::{first_word, second_word}; fn test_strings() { @@ -95,6 +97,44 @@ fn test_files() { 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) + } + + // using std API + match fs::read_to_string("Cargo.toml") { + Ok(s) => println!("Cargo.toml contents:\n{}\n-------------", s), + Err(e) => println!("Could not open Cargo.toml: {:?}", e) + } +} + +fn test_generics() { + let num_list = vec![3, 42, -7, 100, 0]; + println!("largest number: {}", largest(&num_list)); + println!("num_list: {:?}", num_list); + + let char_list = vec!['a', 'y', 'q', 'm']; + println!("largest char: {}", largest(&char_list)); + + let str_list = vec!["hello", "world", "blue", "planet"]; + println!("largest str: {}", largest(&str_list)); + println!("str_list: {:?}", str_list); + + let string_list = vec!["aaaa".to_string(), "xxxxx".to_string(), "ffff".to_string()]; + println!("largest string (with cloning): {}", largest_clone(&string_list)); + println!("largest string (with ref): {}", largest_ref(&string_list)); + println!("string_list: {:?}", string_list); + + let s1 = String::from("abcd"); + let l; + { + let s2 = "efghi"; + l = longest(&s1, s2); + } + println!("longest string: {}", l); } fn main() { @@ -102,4 +142,5 @@ fn main() { test_vectors(); test_hashmaps(); test_files(); + test_generics(); }