X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/2d40516a8393db1f27bb822ff95c71a1a9c82537..ab7f9b241429bd675b437d2437799de75d2f409b:/workspace/src/part01.rs diff --git a/workspace/src/part01.rs b/workspace/src/part01.rs deleted file mode 100644 index be2c2d2..0000000 --- a/workspace/src/part01.rs +++ /dev/null @@ -1,74 +0,0 @@ -// Rust-101, Part 01: Expressions, Inherent methods -// ================================================ - -// For Rust to compile this file, make sure to enable the corresponding line -// in `main.rs` before going on. - - -// ## Expression-based programming -fn sqr(i: i32) -> i32 { i * i } - -// Conditionals are also just expressions. This is comparable to the ternary `? :` operator -// from languages like C. -fn abs(i: i32) -> i32 { if i >= 0 { i } else { -i } } - -enum NumberOrNothing { - Number(i32), - Nothing -} -use self::NumberOrNothing::{Number,Nothing}; -fn number_or_default(n: NumberOrNothing, default: i32) -> i32 { - match n { - Nothing => default, - Number(n) => n, - } -} - -// It is even the case that blocks are expressions, evaluating to the last expression they contain. -fn compute_stuff(x: i32) -> i32 { - let y = { let z = x*x; z + 14 }; - y*y -} - -// Let us now refactor `vec_min`. -fn vec_min(v: Vec) -> NumberOrNothing { - fn min_i32(a: i32, b: i32) -> i32 { - unimplemented!() - } - - let mut min = Nothing; - for e in v { - unimplemented!() - } - min -} - -// Now that's already much shorter! Make sure you can go over the code above and actually understand -// every step of what's going on. - -// ## Inherent implementations -impl NumberOrNothing { - fn print(self) { - match self { - Nothing => println!("The number is: "), - Number(n) => println!("The number is: {}", n), - }; - } -} - -// With our refactored functions and methods, `main` now looks as follows: -fn read_vec() -> Vec { - vec![18,5,7,2,9,27] -} -pub fn main() { - let vec = read_vec(); - let min = vec_min(vec); - unimplemented!() -} -// You will have to replace `part00` by `part01` in the `main` function in -// `main.rs` to run this code. - -// **Exercise 01.1**: Write a function `vec_sum` that computes the sum of all values of a `Vec`. - -// **Exercise 01.2**: Write a function `vec_print` that takes a vector and prints all its elements. -