--- /dev/null
+use std::fs::File;
+use std::io::prelude::*;
+
+pub fn read_file(path: &str) -> Result<String, std::io::Error> {
+ let mut s = String::new();
+ File::open(path)?
+ .read_to_string(&mut s)?;
+ Ok(s)
+}
+
+// needs Copy trait, good for simple types
+pub fn largest<T: PartialOrd + Copy>(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
+pub fn largest_clone<T: PartialOrd + 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
+pub fn largest_ref<T: PartialOrd>(list: &[T]) -> &T {
+ let mut result = &list[0];
+ for i in list {
+ if i > result {
+ result = i;
+ }
+ }
+ result
+}
+
+pub fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
+ if x.len() > y.len() {
+ x
+ } else {
+ y
+ }
+}
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() {
println!("collect_scores after rebuilding with doubling: {:?}", collect_scores);
}
-fn read_file(path: &str) -> Result<String, std::io::Error> {
- 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();
}
}
-// needs Copy trait, good for simple types
-fn largest<T: PartialOrd + Copy>(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<T: PartialOrd + 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<T: PartialOrd>(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));