X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/c3cbdbbd2fff81d2509b22d614343e6ca0250c09..67c07b22fc19f6e39b41bdd5c95b02643f51e81b:/workspace/src/part01.rs?ds=inline diff --git a/workspace/src/part01.rs b/workspace/src/part01.rs index 8d1c8b6..ed6717c 100644 --- a/workspace/src/part01.rs +++ b/workspace/src/part01.rs @@ -1 +1,74 @@ - +// 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. You can compare this 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 funtion `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. +