From: Ralf Jung Date: Tue, 23 Jun 2015 13:23:17 +0000 (+0200) Subject: some more exercises X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/f9bb29fcf0e25966dddd1dca29d70d392a5dfdca?ds=sidebyside some more exercises --- diff --git a/solutions/src/lib.rs b/solutions/src/lib.rs index 8f8d148..aff442c 100644 --- a/solutions/src/lib.rs +++ b/solutions/src/lib.rs @@ -1 +1,2 @@ pub mod bigint; +pub mod vec_min; diff --git a/src/part01.rs b/src/part01.rs index bff079b..05696a5 100644 --- a/src/part01.rs +++ b/src/part01.rs @@ -89,9 +89,8 @@ pub fn main() { // 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_avg` that computes the average value of a `Vec` (rounded down to -// the next integer). -// -// *Hint*: `vec.len() as i32` returns the length of a vector `vec` as signed integer. +// **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. // [index](main.html) | [previous](part00.html) | [next](part02.html) diff --git a/src/part02.rs b/src/part02.rs index 12faed8..57dc83e 100644 --- a/src/part02.rs +++ b/src/part02.rs @@ -3,10 +3,9 @@ use std; -// Let us for a moment reconsider the type `NumberOrNothing`. Isn't it a bit -// annoying that we had to hard-code the type `i32` in there? What if tomorrow, -// we want a `CharOrNothing`, and later a `FloatOrNothing`? Certainly we don't -// want to re-write the type and all its inherent methods. +// Let us for a moment reconsider the type `NumberOrNothing`. Isn't it a bit annoying that we +// had to hard-code the type `i32` in there? What if tomorrow, we want a `CharOrNothing`, and +// later a `FloatOrNothing`? Certainly we don't want to re-write the type and all its inherent methods. // ## Generic datatypes @@ -24,9 +23,8 @@ pub use self::SomethingOrNothing::*; // `SomethingOrNothing` to get back our `NumberOrNothing`. type NumberOrNothing = SomethingOrNothing; // However, we can also write `SomethingOrNothing` or even `SomethingOrNothing>`. -// In fact, such a type is so useful that it is already present in the standard -// library: It's called an *option type*, written `Option`. -// Go check out its [documentation](http://doc.rust-lang.org/stable/std/option/index.html)! +// In fact, such a type is so useful that it is already present in the standard library: It's called an +// *option type*, written `Option`. Go check out its [documentation](http://doc.rust-lang.org/stable/std/option/index.html)! // (And don't worry, there's indeed lots of material mentioned there that we did not cover yet.) // ## Generic `impl`, Static functions @@ -146,4 +144,7 @@ pub fn main() { // If this printed `3`, then you generic `vec_min` is working! So get ready for the next part. +// **Exercise 02.2**: Change your program such that it computes the minimum ofa `Vec` (where `f32` is the type +// of 32-bit floating-point numbers). You should not change `vec_min` in any way, obviously! + // [index](main.html) | [previous](part01.html) | [next](part03.html) diff --git a/src/part03.rs b/src/part03.rs index 0e7db1d..fa82eb7 100644 --- a/src/part03.rs +++ b/src/part03.rs @@ -108,4 +108,7 @@ impl SomethingOrNothing { } } +// **Exercise 03.2**: Building on exercise 02.2, implement all the things you need on `f32` to make your +// program work with floating-point numbers. + // [index](main.html) | [previous](part02.html) | [next](part04.html)