X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=src%2Fmain.rs;h=718ecb5f22028762b437c4b87a60dac13f99c90c;hp=a4e4f1dafd8aad0114dad8d6612f04fe0d7abbfa;hb=f026aa1e762803c4e784f82fd3830c9f936cf8d5;hpb=5ea5b879c2bc3b30ffe56d7806752bf727f98236 diff --git a/src/main.rs b/src/main.rs index a4e4f1d..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::{self, File}; +use lib::*; use word_utils::{first_word, second_word}; fn test_strings() { @@ -69,13 +71,6 @@ fn test_hashmaps() { 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(); @@ -116,47 +111,6 @@ fn test_files() { } } -// needs Copy trait, good for simple types -fn largest(list: &[T]) -> T { - let mut result = list[0]; - for &i in list { - if i > result { - result = i; - } - } - result -} - -// expensive for large strings, don't use that -fn largest_clone(list: &[T]) -> T { - let mut result = list[0].clone(); - for i in list { - if *i > result { - result = i.clone(); - } - } - result -} - -// good for everything, but more expensive for simple types -fn largest_ref(list: &[T]) -> &T { - let mut result = &list[0]; - for i in list { - if i > result { - result = i; - } - } - result -} - -fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { - if x.len() > y.len() { - x - } else { - y - } -} - fn test_generics() { let num_list = vec![3, 42, -7, 100, 0]; println!("largest number: {}", largest(&num_list));