X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/4f61be32dd480f23a7fef05ee66c42ae27c980c6..04e6bfa1aab433e4cfb0058132f7b57258ddcd16:/src/part01.rs diff --git a/src/part01.rs b/src/part01.rs index ed4f73f..e00cf53 100644 --- a/src/part01.rs +++ b/src/part01.rs @@ -13,10 +13,10 @@ //@ 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`! +//@ So we can just write `i * i`, the expression that returns the square of `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 +// Conditionals are also just expressions. This is comparable to the ternary `? :` operator // from languages like C. fn abs(i: i32) -> i32 { if i >= 0 { i } else { -i } } @@ -35,11 +35,18 @@ fn number_or_default(n: NumberOrNothing, default: i32) -> i32 { } } +// It is even the case that blocks are expressions, evaluating to the last expression they contain. +fn compute_stuff(x: i32) -> i32 { + let y = { let z = x*x; z + 14 }; + y*y +} + // 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 + //@ significantly increase readability. fn min_i32(a: i32, b: i32) -> i32 { if a < b { a } else { b } /*@*/ } @@ -80,7 +87,7 @@ impl NumberOrNothing { //@ 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`. +//@ takes `NumberOrNothing` as the first argument, just like `print_number_or_nothing`. //@ //@ Try making `number_or_default` from above an inherent method as well! @@ -96,8 +103,9 @@ pub fn main() { // You will have to replace `part00` by `part01` in the `main` function in // `main.rs` to run this code. -// **Exercise 01.1**: Write a funtion `vec_sum` that computes the sum of all values of a `Vec`. +// **Exercise 01.1**: Write a function `vec_sum` that computes the sum of all values of a `Vec`. // **Exercise 01.2**: Write a function `vec_print` that takes a vector and prints all its elements. -//@ [index](main.html) | [previous](part00.html) | [next](part02.html) +//@ [index](main.html) | [previous](part00.html) | [raw source](workspace/src/part01.rs) | +//@ [next](part02.html)