X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/315bf91eb0b309b29c732ca7726df1f6ca9f567e..3dc9d065dfa259c88b3109717e2c6f6e65a45cc4:/src/part02.rs diff --git a/src/part02.rs b/src/part02.rs index 51c20cf..dc885c0 100644 --- a/src/part02.rs +++ b/src/part02.rs @@ -12,11 +12,12 @@ use std; // meaning "many shapes"). You may know something similar from C++ (where it's called // *templates*) or Java, or one of the many functional languages. So here, we define // a generic type `SomethingOrNothing`. -enum SomethingOrNothing { +pub enum SomethingOrNothing { Something(T), Nothing, } -use self::SomethingOrNothing::{Something,Nothing}; +// Instead of writing out all the variants, we can also just import them all at once. +pub use self::SomethingOrNothing::*; // 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>`. @@ -64,7 +65,7 @@ fn call_constructor(x: i32) -> SomethingOrNothing { // For now, just ignore the `Copy`, we will come back to this point later. // A `trait` is a lot like interfaces in Java: You define a bunch of functions // you want to have implemented, and their argument and return types. -trait Minimum : Copy { +pub trait Minimum : Copy { fn min(a: Self, b: Self) -> Self; } @@ -78,7 +79,7 @@ trait Minimum : Copy { // we cannot call `min`. Just try it! There is no reason to believe that `T` provides such an operation. // This is in strong contrast to C++, where the compiler only checks such details when the // function is actually used. -fn vec_min(v: Vec) -> SomethingOrNothing { +pub fn vec_min(v: Vec) -> SomethingOrNothing { let mut min = Nothing; for e in v { min = Something(match min { @@ -100,7 +101,7 @@ impl Minimum for i32 { // This also shows that we can have multiple `impl` blocks for the same type, and we // can provide some methods only for certain instances of a generic type. impl SomethingOrNothing { - fn print(self) { + pub fn print(self) { match self { Nothing => println!("The number is: "), Something(n) => println!("The number is: {}", n),