ship generated workspace files for the benefit of non-Linux users
[rust-101.git] / workspace / src / part00.rs
1 // ***Remember to enable/add this part in `main.rs`!***
2
3 // Rust-101, Part 00: Algebraic datatypes
4 // ======================================
5
6 // As our first piece of Rust code, we want to write a function that computes the
7 // minimum of a list.
8
9
10 // An `enum` for "a number or nothing" could look as follows:
11 enum NumberOrNothing {
12     Number(i32),
13     Nothing
14 }
15
16 // Observe how in Rust, the return type comes *after* the arguments.
17 fn vec_min(vec: Vec<i32>) -> NumberOrNothing {
18     let mut min = NumberOrNothing::Nothing;
19
20     // Now we want to *iterate* over the list. Rust has some nice syntax for
21     // iterators:
22     for el in vec {
23         // So `el` is al element of the list. We need to update `min` accordingly, but how do we get the current
24         // number in there? This is what pattern matching can do:
25         match min {
26             // In this case (*arm*) of the `match`, `min` is currently nothing, so let's just make it the number `el`.
27             NumberOrNothing::Nothing => {
28                 unimplemented!()
29             },
30             // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it. We will write
31             // the function `min_i32` just after we completed this one.
32             NumberOrNothing::Number(n) => {
33                 unimplemented!()
34             }
35         }
36     }
37     // Finally, we return the result of the computation.
38     return min;
39 }
40
41 // Now that we reduced the problem to computing the minimum of two integers, let's do that.
42 fn min_i32(a: i32, b: i32) -> i32 {
43     if a < b {
44         unimplemented!()
45     } else {
46         unimplemented!()
47     }
48 }
49
50 // Phew. We wrote our first Rust function! But all this `NumberOrNothing::` is getting kind of
51 // ugly. Can't we do that nicer?
52
53 // Indeed, we can: The following line tells Rust to take
54 // the constructors of `NumberOrNothing` into the local namespace.
55 // Try moving that above the function, and removing all the occurrences `NumberOrNothing::`.
56 use self::NumberOrNothing::{Number,Nothing};
57
58 // To call this function, we now just need a list. Of course, ultimately we want to ask the user for
59 // a list of numbers, but for now, let's just hard-code something.
60
61 // `vec!` is a *macro* (as you can tell from the `!`) that constructs a constant `Vec<_>` with the given
62 // elements.
63 fn read_vec() -> Vec<i32> {
64     vec![18,5,7,1,9,27]
65 }
66
67 // Finally, let's call our functions and run the code!
68 // But, wait, we would like to actually see something, so we need to print the result.
69 // Of course Rust can print numbers, but after calling `vec_min`, we have a `NumberOrNothing`.
70 // So let's write a small helper function that prints such values.
71
72 fn print_number_or_nothing(n: NumberOrNothing) {
73     match n {
74         Nothing => println!("The number is: <nothing>"),
75         Number(n) => println!("The number is: {}", n),
76     };
77 }
78
79 // Putting it all together:
80 pub fn main() {
81     let vec = read_vec();
82     let min = vec_min(vec);
83     print_number_or_nothing(min);
84 }
85
86 // Now try `cargo run` on the console to run above code.
87
88
89 // [index](main.html) | previous | [next](part01.html)