@@ -195,9 +195,11 @@ The second point is the `println!()` part. This is calling a Rust **macro**,
195
195
which is how metaprogramming is done in Rust. If it were a function instead, it
196
196
would look like this: ` println() ` . For our purposes, we don't need to worry
197
197
about this difference. Just know that sometimes, you'll see a ` ! ` , and that
198
- means that you're calling a macro instead of a normal function. One last thing
199
- to mention: Rust's macros are significantly different than C macros, if you've
200
- used those. Don't be scared of using macros. We'll get to the details
198
+ means that you're calling a macro instead of a normal function. Rust implements
199
+ ` println! ` as a macro rather than a function for good reasons, but that's a
200
+ very advanced topic. You'll learn more when we talk about macros later. One
201
+ last thing to mention: Rust's macros are significantly different than C macros,
202
+ if you've used those. Don't be scared of using macros. We'll get to the details
201
203
eventually, you'll just have to trust us for now.
202
204
203
205
Next, ` "Hello, world!" ` is a ** string** . Strings are a surprisingly complicated
@@ -659,14 +661,12 @@ error: mismatched types: expected `int` but found `()` (expected int but found (
659
661
```
660
662
661
663
We expected an integer, but we got ` () ` . ` () ` is pronounced 'unit', and is a
662
- special type in Rust's type system. ` () ` is different than ` null ` in other
663
- languages, because ` () ` is distinct from other types. For example, in C, ` null `
664
- is a valid value for a variable of type ` int ` . In Rust, ` () ` is _ not_ a valid
665
- value for a variable of type ` int ` . It's only a valid value for variables of
666
- the type ` () ` , which aren't very useful. Remember how we said statements don't
667
- return a value? Well, that's the purpose of unit in this case. The semicolon
668
- turns any expression into a statement by throwing away its value and returning
669
- unit instead.
664
+ special type in Rust's type system. In Rust, ` () ` is _ not_ a valid value for a
665
+ variable of type ` int ` . It's only a valid value for variables of the type ` () ` ,
666
+ which aren't very useful. Remember how we said statements don't return a value?
667
+ Well, that's the purpose of unit in this case. The semicolon turns any
668
+ expression into a statement by throwing away its value and returning unit
669
+ instead.
670
670
671
671
There's one more time in which you won't see a semicolon at the end of a line
672
672
of Rust code. For that, we'll need our next concept: functions.
@@ -1680,11 +1680,11 @@ just `int`s.
1680
1680
1681
1681
Rust provides a method on these ` IoResult<T> ` s called ` ok() ` , which does the
1682
1682
same thing as our ` match ` statement, but assuming that we have a valid value.
1683
- If we don't, it will terminate our program. In this case, if we can't get
1684
- input, our program doesn 't work, so we're okay with that . In most cases, we
1685
- would want to handle the error case explicitly. The result of ` ok() ` has a
1686
- method, ` expect() ` , which allows us to give an error message if this crash
1687
- happens.
1683
+ We then call ` expect() ` on the result, which will terminate our program if we
1684
+ don 't have a valid value . In this case, if we can't get input, our program
1685
+ doesn't work, so we're okay with that. In most cases, we would want to handle
1686
+ the error case explicitly. ` expect() ` allows us to give an error message if
1687
+ this crash happens.
1688
1688
1689
1689
We will cover the exact details of how all of this works later in the Guide.
1690
1690
For now, this gives you enough of a basic understanding to work with.
@@ -2030,7 +2030,7 @@ fn main() {
2030
2030
match cmp(input, secret_number) {
2031
2031
Less => println!("Too small!"),
2032
2032
Greater => println!("Too big!"),
2033
- Equal => { println!("You win!"); } ,
2033
+ Equal => println!("You win!"),
2034
2034
}
2035
2035
}
2036
2036
@@ -2727,7 +2727,8 @@ mod hello {
2727
2727
}
2728
2728
```
2729
2729
2730
- This will work:
2730
+ Usage of the ` pub ` keyword is sometimes called 'exporting', because
2731
+ we're making the function available for other modules. This will work:
2731
2732
2732
2733
``` {notrust,ignore}
2733
2734
$ cargo run
@@ -3291,8 +3292,7 @@ use super::times_four;
3291
3292
3292
3293
Because we've made a nested module, we can import functions from the parent
3293
3294
module by using ` super ` . Sub-modules are allowed to 'see' private functions in
3294
- the parent. We sometimes call this usage of ` use ` a 're-export,' because we're
3295
- exporting the name again, somewhere else.
3295
+ the parent.
3296
3296
3297
3297
We've now covered the basics of testing. Rust's tools are primitive, but they
3298
3298
work well in the simple cases. There are some Rustaceans working on building
0 commit comments