// [the first part](part00.html), or jump directly to where you left off:
//
// * [Part 00](part00.html)
-// * [Part 01](part01.html) (WIP)
+// * [Part 01](part01.html)
+// * [Part 02](part02.html) (WIP)
// * (to be continued)
#![allow(dead_code)]
mod part00;
mod part01;
+mod part02;
// To actually run the code of some part (after filling in the blanks, if necessary), simply edit the `main`
// function below.
-// Rust-101, Part 00: Expressions, Inherent methods
+// Rust-101, Part 01: Expressions, Inherent methods
// ================================================
use std;
// understand that Rust is an "expression-based" language. This means that most of the
// terms you write down are not just *statements* (executing code), but *expressions*
// (returning a value). This applies even to the body of entire functions!
-
+//
// For example, consider `sqr`:
fn sqr(i: i32) -> i32 { i * i }
// Between the curly braces, we are giving the *expression* that computes the return value.
// So we can just write `i * i`, the expression that returns the square if `i`!
// This is very close to how mathematicians write down functions (but with more types).
-
+//
// 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 } }
// You will have to replace `part00` by `part01` in the `main` function in
// `main.rs` to run this code.
+// **Exercise**: Write a funtion `vec_avg` that computes the average value of a `Vec<i32>`.
+// *Hint*: `vec.len()` returns the length of a vector `vec`.
+
// [index](main.html) | [previous](part00.html) | [next](part02.html)
--- /dev/null
+// Rust-101, Part 02: Generic types (WIP)
+// ================================
+
+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.
+//
+// The solution to this is called *generics* or *polymorphism* (the latter is Greek,
+// meaning "many shapes"). You may know something similar from C++ (where it's called
+// *templates*) or Java, or one of the many functional languages. A generic
+// `SomethingOrNothing` type looks as follows:
+enum SomethingOrNothing<T> {
+ Something(T),
+ Nothing,
+}
+use self::SomethingOrNothing::{Something,Nothing};
+// What this does is to define an entire family of types: We can now write
+// `SomethingOrNothing<i32>` to get back our `NumberOrNothing`, but we
+// can also write `SomethingOrNothing<bool>` or even `SomethingOrNothing<SomethingOrNothing<i32>>`.
+// 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<T>`.
+// 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.)
+
+// [index](main.html) | [previous](part01.html) | [next](part03.html)