From: Ralf Jung Date: Fri, 26 Jun 2015 17:57:33 +0000 (+0200) Subject: @-ify part 01; update main for non-Linux users X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/a115b75de6e7e85f8799a77e2998ab1a24743e06?ds=sidebyside;hp=229b86d07e94cd3ec175051a44b3f3cb45b40b65 @-ify part 01; update main for non-Linux users --- diff --git a/src/main.rs b/src/main.rs index 2f4b705..4ecf632 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,14 +5,10 @@ // tutorial for the [Rust language](http://www.rust-lang.org/). // It is intended to be an interactive, hands-on course: I believe the only way to // *really* learn a language is to write code in it, so you should be coding during -// the course. I am writing this with a tutorial situation in mind, i.e., -// with a teacher being around to guide students through the course and answer -// questions as they come up. However, I think they may also be useful if you -// work through them on your own, you will just have to show more initiative yourself: -// Make sure you actually type some code. It may sound stupid to manually copy code -// that you could duplicate through the clipboard, but it's actually helpful. -// If you have questions, check out the "Additional Resources" below. In particular, -// the IRC channel is filled with awesome people willing to help you! I spent +// the course. +// +// If you have any questions that are not answered here, check out the "Additional Resources" +// below. In particular, the IRC channel is filled with awesome people willing to help you! I spent // lots of time there ;-) // // I will assume some familiarity with programming, and hence not explain the basic @@ -30,7 +26,7 @@ // cost. This is combined with the comfort of high-level functional languages and guaranteed // safety (as in, the program will not crash). The vast majority of existing // languages sacrificies one of these goals for the other. In particular, the -// first requirement rules out a garbage collector: Rust can run "mare metal". +// first requirement rules out a garbage collector: Rust can run "bare metal". // In fact, Rust rules out more classes of bugs than languages that achieve safety // with a GC: Besides dangling pointers and double-free, Rust also prevents issues // such as iterator invalidation and race conditions. @@ -43,17 +39,18 @@ // [the Rust website](http://www.rust-lang.org/). You should go for either the "stable" // or the "beta" channel. More detailed installation instructions are provided in // [the second chapter of The Book](https://doc.rust-lang.org/stable/book/installing-rust.html). +// This will also install `cargo`, the tool responsible for building rust projects (or *crates*). // Next, fetch the Rust-101 source code from the [git repository](http://www.ralfj.de/git/rust-101.git) -// (also available [on GitHub](https://github.com/RalfJung/rust-101)). -// To generate your workspace, run `make workspace` (this needs GNU sed). I suggest you now copy the -// `workspace` folder somewhere else - that will make it much easier to later update the course without +// (also available [on GitHub](https://github.com/RalfJung/rust-101), and as [zip archive](https://github.com/RalfJung/rust-101/archive/master.zip)). +// There is a workspace prepared for you in the `workspace` folder. I suggest you copy this +// folder somewhere else - that will make it much easier to later update the course without // overwriting your changes. Try `cargo build` in that new folder to check that compiling your workspace succeeds. // (You can also execute it with `cargo run`, but you'll need to do some work before this will succeed.) // -// If you later want to update the course, do `git pull` followed by `make workspace`. Then copy the files -// from `workspace/src/` to your workspace that you did not yet work on. (Of course you can also copy the rest, -// but that would replace all your hard work by the original files with all the holes!) +// If you later want to update the course, do `git pull` (or re-download the tip archive). +// Then copy the files from `workspace/src/` to your workspace that you did not yet work on. (Of course you can also +// copy the rest, but that would replace all your hard work by the original files with all the holes!) // Course Content // -------------- @@ -62,7 +59,7 @@ // mechanisms like pattern matching and traits. Parts 04-06 introduce the heart of the language, the ideas // making it different from anything else out there: Ownership, borrowing, lifetimes. In part 07-??, we // continue our tour through Rust with another example. Finally, in parts ??-??, we implement our own -// version of `grep`, exhibiting useful Rust features as we go. +// version of `grep`, exhibiting some more Rust features as we go. // // Now, open `your-workspace/src/part00.rs` in your favorite editor, and follow the link below for // the explanations and exercises. Have fun! diff --git a/src/part00.rs b/src/part00.rs index 2bc40d9..ed76e81 100644 --- a/src/part00.rs +++ b/src/part00.rs @@ -45,8 +45,8 @@ fn vec_min(vec: Vec) -> NumberOrNothing { NumberOrNothing::Nothing => { min = NumberOrNothing::Number(el); /*@*/ }, - // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it. We will write - // the function `min_i32` just after we completed this one. + // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it. + //@ We will write the function `min_i32` just after we completed this one. NumberOrNothing::Number(n) => { let new_min = min_i32(n, el); /*@*/ min = NumberOrNothing::Number(new_min); /*@*/ @@ -77,10 +77,10 @@ use self::NumberOrNothing::{Number,Nothing}; // To call this function, we now just need a list. Of course, ultimately we want to ask the user for // a list of numbers, but for now, let's just hard-code something. -// `vec!` is a *macro* (as you can tell from the `!`) that constructs a constant `Vec<_>` with the given -// elements. +//@ `vec!` is a *macro* (as you can tell from the `!`) that constructs a constant `Vec<_>` with the given +//@ elements. fn read_vec() -> Vec { - vec![18,5,7,1,9,27] + vec![18,5,7,1,9,27] /*@*/ } // Finally, let's call our functions and run the code! @@ -92,10 +92,10 @@ fn read_vec() -> Vec { //@ now, you just need to know that `{}` is the placeholder for a value, and that Rust //@ will check at compile-time that you supplied the right number of arguments. fn print_number_or_nothing(n: NumberOrNothing) { - match n { - Nothing => println!("The number is: "), - Number(n) => println!("The number is: {}", n), - }; + match n { /*@*/ + Nothing => println!("The number is: "), /*@*/ + Number(n) => println!("The number is: {}", n), /*@*/ + }; /*@*/ } // Putting it all together: @@ -105,10 +105,11 @@ pub fn main() { print_number_or_nothing(min); } -// Now try `cargo run` on the console to run above code. +// You can now use `cargo build` to compile your code. If all goes well, try `cargo run` on the +// console to run it. //@ Yay, it said "1"! That's actually the right answer. Okay, we could have //@ computed that ourselves, but that's besides the point. More importantly: //@ You completed the first part of the course. -// [index](main.html) | previous | [next](part01.html) +//@ [index](main.html) | previous | [next](part01.html) diff --git a/src/part01.rs b/src/part01.rs index bb3e919..11e9077 100644 --- a/src/part01.rs +++ b/src/part01.rs @@ -1,26 +1,28 @@ // Rust-101, Part 01: Expressions, Inherent methods // ================================================ -// Even though our code from the first part works, we can still learn a -// lot by making it prettier. To understand how, it is important to -// 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 Rust to compile this file, make sure to enable the corresponding line +// in `main.rs` before going on. + +//@ Even though our code from the first part works, we can still learn a +//@ lot by making it prettier. That's because Rust is an "expression-based" language, which +//@ 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! // ## Expression-based programming -// For example, consider `sqr`: +//@ 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). +//@ 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 } } -// And the same applies to case distinction with `match`: Every `arm` of the match -// gives the expression that is returned in the respective case. -// (We repeat the definition from the previous part here.) +//@ And the same applies to case distinction with `match`: Every `arm` of the match +//@ gives the expression that is returned in the respective case. +//@ (We repeat the definition from the previous part here.) enum NumberOrNothing { Number(i32), Nothing @@ -35,26 +37,26 @@ fn number_or_default(n: NumberOrNothing, default: i32) -> i32 { // Let us now refactor `vec_min`. fn vec_min(v: Vec) -> NumberOrNothing { - // Remember that helper function `min_i32`? Rust allows us to define such helper functions *inside* other - // functions. This is just a matter of namespacing, the inner function has no access to the data of the outer - // one. Still, being able to nicely group functions can be very useful. + //@ Remember that helper function `min_i32`? Rust allows us to define such helper functions *inside* other + //@ functions. This is just a matter of namespacing, the inner function has no access to the data of the outer + //@ one. Still, being able to nicely group functions can be very useful. fn min_i32(a: i32, b: i32) -> i32 { - if a < b { a } else { b } + if a < b { a } else { b } /*@*/ } let mut min = Nothing; for e in v { - // Notice that all we do here is compute a new value for `min`, and that it will always end - // up being a `Number` rather than `Nothing`. In Rust, the structure of the code - // can express this uniformity. - min = Number(match min { - Nothing => e, - Number(n) => min_i32(n, e) - }); + //@ Notice that all we do here is compute a new value for `min`, and that it will always end + //@ up being a `Number` rather than `Nothing`. In Rust, the structure of the code + //@ can express this uniformity. + min = Number(match min { /*@*/ + Nothing => e, /*@*/ + Number(n) => min_i32(n, e) /*@*/ + }); /*@*/ } - // The `return` keyword exists in Rust, but it is rarely used. Instead, we typically - // make use of the fact that the entire function body is an expression, so we can just - // write down the desired return value. + //@ The `return` keyword exists in Rust, but it is rarely used. Instead, we typically + //@ make use of the fact that the entire function body is an expression, so we can just + //@ write down the desired return value. min } @@ -62,10 +64,10 @@ fn vec_min(v: Vec) -> NumberOrNothing { // every step of what's going on. // ## Inherent implementations -// So much for `vec_min`. Let us now reconsider `print_number_or_nothing`. That function -// really belongs pretty close to the type `NumberOrNothing`. In C++ or Java, you would -// probably make it a method of the type. In Rust, we can achieve something very similar -// by providing an *inherent implementation*. +//@ So much for `vec_min`. Let us now reconsider `print_number_or_nothing`. That function +//@ really belongs pretty close to the type `NumberOrNothing`. In C++ or Java, you would +//@ probably make it a method of the type. In Rust, we can achieve something very similar +//@ by providing an *inherent implementation*. impl NumberOrNothing { fn print(self) { match self { @@ -74,13 +76,13 @@ impl NumberOrNothing { }; } } -// So, what just happened? Rust separates code from data, so the definition of the -// methods on an `enum` (and also on `struct`, which we will learn about later) -// is independent of the definition of the type. `self` is like `this` in other -// languages, and its type is always implicit. So `print` is now a method that -// takes as first argument a `NumberOrNothing`, just like `print_number_or_nothing`. -// -// Try making `number_or_default` from above an inherent method as well! +//@ So, what just happened? Rust separates code from data, so the definition of the +//@ methods on an `enum` (and also on `struct`, which we will learn about later) +//@ is independent of the definition of the type. `self` is like `this` in other +//@ languages, and its type is always implicit. So `print` is now a method that +//@ takes as first argument a `NumberOrNothing`, just like `print_number_or_nothing`. +//@ +//@ Try making `number_or_default` from above an inherent method as well! // With our refactored functions and methods, `main` now looks as follows: fn read_vec() -> Vec { @@ -89,7 +91,7 @@ fn read_vec() -> Vec { pub fn main() { let vec = read_vec(); let min = vec_min(vec); - min.print(); + min.print(); /*@*/ } // You will have to replace `part00` by `part01` in the `main` function in // `main.rs` to run this code. diff --git a/workspace/src/part00.rs b/workspace/src/part00.rs index 4f7b403..2998b20 100644 --- a/workspace/src/part00.rs +++ b/workspace/src/part00.rs @@ -27,8 +27,7 @@ fn vec_min(vec: Vec) -> NumberOrNothing { NumberOrNothing::Nothing => { unimplemented!() }, - // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it. We will write - // the function `min_i32` just after we completed this one. + // In this arm, `min` is currently the number `n`, so let's compute the new minimum and store it. NumberOrNothing::Number(n) => { unimplemented!() } @@ -58,10 +57,8 @@ use self::NumberOrNothing::{Number,Nothing}; // To call this function, we now just need a list. Of course, ultimately we want to ask the user for // a list of numbers, but for now, let's just hard-code something. -// `vec!` is a *macro* (as you can tell from the `!`) that constructs a constant `Vec<_>` with the given -// elements. fn read_vec() -> Vec { - vec![18,5,7,1,9,27] + unimplemented!() } // Finally, let's call our functions and run the code! @@ -70,10 +67,7 @@ fn read_vec() -> Vec { // So let's write a small helper function that prints such values. fn print_number_or_nothing(n: NumberOrNothing) { - match n { - Nothing => println!("The number is: "), - Number(n) => println!("The number is: {}", n), - }; + unimplemented!() } // Putting it all together: @@ -83,7 +77,7 @@ pub fn main() { print_number_or_nothing(min); } -// Now try `cargo run` on the console to run above code. +// You can now use `cargo build` to compile your code. If all goes well, try `cargo run` on the +// console to run it. -// [index](main.html) | previous | [next](part01.html) diff --git a/workspace/src/part01.rs b/workspace/src/part01.rs index da94915..bcc1b03 100644 --- a/workspace/src/part01.rs +++ b/workspace/src/part01.rs @@ -3,26 +3,17 @@ // Rust-101, Part 01: Expressions, Inherent methods // ================================================ -// Even though our code from the first part works, we can still learn a -// lot by making it prettier. To understand how, it is important to -// 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 Rust to compile this file, make sure to enable the corresponding line +// in `main.rs` before going on. + // ## Expression-based programming -// 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 } } -// And the same applies to case distinction with `match`: Every `arm` of the match -// gives the expression that is returned in the respective case. -// (We repeat the definition from the previous part here.) enum NumberOrNothing { Number(i32), Nothing @@ -37,26 +28,14 @@ fn number_or_default(n: NumberOrNothing, default: i32) -> i32 { // Let us now refactor `vec_min`. fn vec_min(v: Vec) -> NumberOrNothing { - // Remember that helper function `min_i32`? Rust allows us to define such helper functions *inside* other - // functions. This is just a matter of namespacing, the inner function has no access to the data of the outer - // one. Still, being able to nicely group functions can be very useful. fn min_i32(a: i32, b: i32) -> i32 { - if a < b { a } else { b } + unimplemented!() } let mut min = Nothing; for e in v { - // Notice that all we do here is compute a new value for `min`, and that it will always end - // up being a `Number` rather than `Nothing`. In Rust, the structure of the code - // can express this uniformity. - min = Number(match min { - Nothing => e, - Number(n) => min_i32(n, e) - }); + unimplemented!() } - // The `return` keyword exists in Rust, but it is rarely used. Instead, we typically - // make use of the fact that the entire function body is an expression, so we can just - // write down the desired return value. min } @@ -64,10 +43,6 @@ fn vec_min(v: Vec) -> NumberOrNothing { // every step of what's going on. // ## Inherent implementations -// So much for `vec_min`. Let us now reconsider `print_number_or_nothing`. That function -// really belongs pretty close to the type `NumberOrNothing`. In C++ or Java, you would -// probably make it a method of the type. In Rust, we can achieve something very similar -// by providing an *inherent implementation*. impl NumberOrNothing { fn print(self) { match self { @@ -76,13 +51,6 @@ impl NumberOrNothing { }; } } -// So, what just happened? Rust separates code from data, so the definition of the -// methods on an `enum` (and also on `struct`, which we will learn about later) -// is independent of the definition of the type. `self` is like `this` in other -// languages, and its type is always implicit. So `print` is now a method that -// takes as first argument a `NumberOrNothing`, just like `print_number_or_nothing`. -// -// Try making `number_or_default` from above an inherent method as well! // With our refactored functions and methods, `main` now looks as follows: fn read_vec() -> Vec { @@ -91,7 +59,7 @@ fn read_vec() -> Vec { pub fn main() { let vec = read_vec(); let min = vec_min(vec); - min.print(); + unimplemented!() } // You will have to replace `part00` by `part01` in the `main` function in // `main.rs` to run this code.