From 53260b4082f1c76b657845560c273b853a166fab Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 9 Jun 2015 11:21:12 +0200 Subject: [PATCH] start in part 02 --- src/main.rs | 4 +++- src/part01.rs | 9 ++++++--- src/part02.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/part02.rs diff --git a/src/main.rs b/src/main.rs index 09a16d1..7850a99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,11 +39,13 @@ // [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. diff --git a/src/part01.rs b/src/part01.rs index 36e15d0..7c205bd 100644 --- a/src/part01.rs +++ b/src/part01.rs @@ -1,4 +1,4 @@ -// Rust-101, Part 00: Expressions, Inherent methods +// Rust-101, Part 01: Expressions, Inherent methods // ================================================ use std; @@ -8,13 +8,13 @@ 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 } } @@ -92,4 +92,7 @@ pub fn part_main() { // 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`. +// *Hint*: `vec.len()` returns the length of a vector `vec`. + // [index](main.html) | [previous](part00.html) | [next](part02.html) diff --git a/src/part02.rs b/src/part02.rs new file mode 100644 index 0000000..32fbe0a --- /dev/null +++ b/src/part02.rs @@ -0,0 +1,28 @@ +// 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 { + Something(T), + Nothing, +} +use self::SomethingOrNothing::{Something,Nothing}; +// What this does is to define an entire family of types: We can now write +// `SomethingOrNothing` to get back our `NumberOrNothing`, but 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)! +// (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) -- 2.30.2