@@ -130,14 +130,15 @@ the documentation for your shell for more details.
130
130
131
131
Let's make a new source file next. I'm going to use the syntax `editor
132
132
filename` to represent editing a file in these examples, but you should use
133
- whatever method you want. We'll call our file ` hello_world .rs` :
133
+ whatever method you want. We'll call our file ` main .rs` :
134
134
135
135
``` {bash}
136
- $ editor hello_world .rs
136
+ $ editor main .rs
137
137
```
138
138
139
139
Rust files always end in a ` .rs ` extension. If you're using more than one word
140
- in your file name, use an underscore. ` hello_world.rs ` versus ` goodbye.rs ` .
140
+ in your file name, use an underscore. ` hello_world.rs ` rather than
141
+ ` helloworld.rs ` .
141
142
142
143
Now that you've got your file open, type this in:
143
144
@@ -150,7 +151,7 @@ fn main() {
150
151
Save the file, and then type this into your terminal window:
151
152
152
153
``` {bash}
153
- $ rustc hello_world .rs
154
+ $ rustc main .rs
154
155
$ ./hello_world # or hello_world.exe on Windows
155
156
Hello, world!
156
157
```
@@ -212,22 +213,22 @@ Finally, actually **compiling** and **running** our program. We can compile
212
213
with our compiler, ` rustc ` , by passing it the name of our source file:
213
214
214
215
``` {bash}
215
- $ rustc hello_world .rs
216
+ $ rustc main .rs
216
217
```
217
218
218
219
This is similar to ` gcc ` or ` clang ` , if you come from a C or C++ background. Rust
219
220
will output a binary executable. You can see it with ` ls ` :
220
221
221
222
``` {bash}
222
223
$ ls
223
- hello_world hello_world .rs
224
+ main main .rs
224
225
```
225
226
226
227
Or on Windows:
227
228
228
229
``` {bash}
229
230
$ dir
230
- hello_world .exe hello_world .rs
231
+ main .exe main .rs
231
232
```
232
233
233
234
There are now two files: our source code, with the ` .rs ` extension, and the
@@ -284,7 +285,7 @@ do that part first:
284
285
285
286
``` {bash}
286
287
$ mkdir src
287
- $ mv hello_world .rs src/hello_world .rs
288
+ $ mv main .rs src/main .rs
288
289
```
289
290
290
291
Cargo expects your source files to live inside a ` src ` directory. That leaves
@@ -452,9 +453,9 @@ let x;
452
453
...we'll get an error:
453
454
454
455
``` {ignore}
455
- src/hello_world .rs:2:9: 2:10 error: cannot determine a type for this local variable: unconstrained type
456
- src/hello_world .rs:2 let x;
457
- ^
456
+ src/main .rs:2:9: 2:10 error: cannot determine a type for this local variable: unconstrained type
457
+ src/main .rs:2 let x;
458
+ ^
458
459
```
459
460
460
461
Giving it a type will compile, though:
@@ -463,7 +464,7 @@ Giving it a type will compile, though:
463
464
let x: int;
464
465
```
465
466
466
- Let's try it out. Change your ` src/hello_world .rs ` file to look like this:
467
+ Let's try it out. Change your ` src/main .rs ` file to look like this:
467
468
468
469
``` {rust}
469
470
fn main() {
@@ -478,8 +479,8 @@ but it will still print "Hello, world!":
478
479
479
480
``` {ignore,notrust}
480
481
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
481
- src/hello_world .rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)] on by default
482
- src/hello_world .rs:2 let x: int;
482
+ src/main .rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)] on by default
483
+ src/main .rs:2 let x: int;
483
484
^
484
485
```
485
486
@@ -500,13 +501,13 @@ And try to build it. You'll get an error:
500
501
``` {bash}
501
502
$ cargo build
502
503
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
503
- src/hello_world .rs:4:39: 4:40 error: use of possibly uninitialized variable: `x`
504
- src/hello_world .rs:4 println!("The value of x is: {}", x);
505
- ^
504
+ src/main .rs:4:39: 4:40 error: use of possibly uninitialized variable: `x`
505
+ src/main .rs:4 println!("The value of x is: {}", x);
506
+ ^
506
507
note: in expansion of format_args!
507
508
<std macros>:2:23: 2:77 note: expansion site
508
509
<std macros>:1:1: 3:2 note: in expansion of println!
509
- src/hello_world .rs:4:5: 4:42 note: expansion site
510
+ src/main .rs:4:5: 4:42 note: expansion site
510
511
error: aborting due to previous error
511
512
Could not compile `hello_world`.
512
513
```
@@ -1318,10 +1319,7 @@ upper bound is exclusive, though, so our loop will print `0` through `9`, not
1318
1319
1319
1320
Rust does not have the "C style" ` for ` loop on purpose. Manually controlling
1320
1321
each element of the loop is complicated and error prone, even for experienced C
1321
- developers. There's an old joke that goes, "There are two hard problems in
1322
- computer science: naming things, cache invalidation, and off-by-one errors."
1323
- The joke, of course, being that the setup says "two hard problems" but then
1324
- lists three things. This happens quite a bit with "C style" ` for ` loops.
1322
+ developers.
1325
1323
1326
1324
We'll talk more about ` for ` when we cover ** iterator** s, later in the Guide.
1327
1325
@@ -2745,197 +2743,8 @@ $ cargo run
2745
2743
Hello, world!
2746
2744
```
2747
2745
2748
- Nice!
2749
-
2750
- There's a common pattern when you're building an executable: you build both an
2751
- executable and a library, and put most of your logic in the library. That way,
2752
- other programs can use that library to build their own functionality.
2753
-
2754
- Let's do that with our project. If you remember, libraries and executables
2755
- are both crates, so while our project has one crate now, let's make a second:
2756
- one for the library, and one for the executable.
2757
-
2758
- To make the second crate, open up ` src/lib.rs ` and put this code in it:
2759
-
2760
- ``` {rust}
2761
- mod hello {
2762
- pub fn print_hello() {
2763
- println!("Hello, world!");
2764
- }
2765
- }
2766
- ```
2767
-
2768
- And change your ` src/main.rs ` to look like this:
2769
-
2770
- ``` {rust,ignore}
2771
- extern crate modules;
2772
-
2773
- fn main() {
2774
- modules::hello::print_hello();
2775
- }
2776
- ```
2777
-
2778
- There's been a few changes. First, we moved our ` hello ` module into its own
2779
- file, ` src/lib.rs ` . This is the file that Cargo expects a library crate to
2780
- be named, by convention.
2781
-
2782
- Next, we added an ` extern crate modules ` to the top of our ` src/main.rs ` . This,
2783
- as you can guess, lets Rust know that our crate relies on another, external
2784
- crate. We also had to modify our call to ` print_hello ` : now that it's in
2785
- another crate, we need to specify that crate first.
2786
-
2787
- This doesn't _ quite_ work yet. Try it:
2788
-
2789
- ``` {notrust,ignore}
2790
- $ cargo build
2791
- Compiling modules v0.0.1 (file:///home/you/projects/modules)
2792
- /home/you/projects/modules/src/lib.rs:2:5: 4:6 warning: code is never used: `print_hello`, #[warn(dead_code)] on by default
2793
- /home/you/projects/modules/src/lib.rs:2 pub fn print_hello() {
2794
- /home/you/projects/modules/src/lib.rs:3 println!("Hello, world!");
2795
- /home/you/projects/modules/src/lib.rs:4 }
2796
- /home/you/projects/modules/src/main.rs:4:5: 4:32 error: function `print_hello` is private
2797
- /home/you/projects/modules/src/main.rs:4 modules::hello::print_hello();
2798
- ^~~~~~~~~~~~~~~~~~~~~~~~~~~
2799
- error: aborting due to previous error
2800
- Could not compile `modules`.
2801
- ```
2802
-
2803
- First, we get a warning that some code is never used. Odd. Next, we get an error:
2804
- ` print_hello ` is private, so we can't call it. Notice that the first error came
2805
- from ` src/lib.rs ` , and the second came from ` src/main.rs ` : cargo is smart enough
2806
- to build it all with one command. Also, after seeing the second error, the warning
2807
- makes sense: we never actually call ` hello_world ` , because we're not allowed to!
2808
-
2809
- Just like modules, crates also have private visibility by default. Any modules
2810
- inside of a crate can only be used by other modules in the crate, unless they
2811
- use ` pub ` . In ` src/lib.rs ` , change this line:
2812
-
2813
- ``` {rust,ignore}
2814
- mod hello {
2815
- ```
2816
-
2817
- To this:
2818
-
2819
- ``` {rust,ignore}
2820
- pub mod hello {
2821
- ```
2822
-
2823
- And everything should work:
2824
-
2825
- ``` {notrust,ignore}
2826
- $ cargo run
2827
- Compiling modules v0.0.1 (file:///home/you/projects/modules)
2828
- Running `target/modules`
2829
- Hello, world!
2830
- ```
2831
-
2832
- Let's do one more thing: add a ` goodbye ` module as well. Imagine a ` src/lib.rs `
2833
- that looks like this:
2834
-
2835
- ``` {rust,ignore}
2836
- pub mod hello {
2837
- pub fn print_hello() {
2838
- println!("Hello, world!");
2839
- }
2840
- }
2841
-
2842
- pub mod goodbye {
2843
- pub fn print_goodbye() {
2844
- println!("Goodbye for now!");
2845
- }
2846
- }
2847
- ```
2848
-
2849
- Now, these two modules are pretty small, but imagine we've written a real, large
2850
- program: they could both be huge. So maybe we want to move them into their own
2851
- files. We can do that pretty easily, and there are two different conventions
2852
- for doing it. Let's give each a try. First, make ` src/lib.rs ` look like this:
2853
-
2854
- ``` {rust,ignore}
2855
- pub mod hello;
2856
- pub mod goodbye;
2857
- ```
2858
-
2859
- This tells Rust that this crate has two public modules: ` hello ` and ` goodbye ` .
2860
-
2861
- Next, make a ` src/hello.rs ` that contains this:
2862
-
2863
- ``` {rust,ignore}
2864
- pub fn print_hello() {
2865
- println!("Hello, world!");
2866
- }
2867
- ```
2868
-
2869
- When we include a module like this, we don't need to make the ` mod ` declaration
2870
- in ` hello.rs ` , because it's already been declared in ` lib.rs ` . ` hello.rs ` just
2871
- contains the body of the module which is defined (by the ` pub mod hello ` ) in
2872
- ` lib.rs ` . This helps prevent 'rightward drift': when you end up indenting so
2873
- many times that your code is hard to read.
2874
-
2875
- Finally, make a new directory, ` src/goodbye ` , and make a new file in it,
2876
- ` src/goodbye/mod.rs ` :
2877
-
2878
- ``` {rust,ignore}
2879
- pub fn print_goodbye() {
2880
- println!("Bye for now!");
2881
- }
2882
- ```
2883
-
2884
- Same deal, but we can make a folder with a ` mod.rs ` instead of ` mod_name.rs ` in
2885
- the same directory. If you have a lot of modules, nested folders can make
2886
- sense. For example, if the ` goodbye ` module had its _ own_ modules inside of
2887
- it, putting all of that in a folder helps keep our directory structure tidy.
2888
- And in fact, if you place the modules in separate files, they're required to be
2889
- in separate folders.
2890
-
2891
- This should all compile as usual:
2892
-
2893
- ``` {notrust,ignore}
2894
- $ cargo build
2895
- Compiling modules v0.0.1 (file:///home/you/projects/modules)
2896
- ```
2897
-
2898
- We've seen how the ` :: ` operator can be used to call into modules, but when
2899
- we have deep nesting like ` modules::hello::say_hello ` , it can get tedious.
2900
- That's why we have the ` use ` keyword.
2901
-
2902
- ` use ` allows us to bring certain names into another scope. For example, here's
2903
- our main program:
2904
-
2905
- ``` {rust,ignore}
2906
- extern crate modules;
2907
-
2908
- fn main() {
2909
- modules::hello::print_hello();
2910
- }
2911
- ```
2912
-
2913
- We could instead write this:
2914
-
2915
- ``` {rust,ignore}
2916
- extern crate modules;
2917
-
2918
- use modules::hello::print_hello;
2919
-
2920
- fn main() {
2921
- print_hello();
2922
- }
2923
- ```
2924
-
2925
- By bringing ` print_hello ` into scope, we don't need to qualify it anymore. However,
2926
- it's considered proper style to do write this code like like this:
2927
-
2928
- ``` {rust,ignore}
2929
- extern crate modules;
2930
-
2931
- use modules::hello;
2932
-
2933
- fn main() {
2934
- hello::print_hello();
2935
- }
2936
- ```
2937
-
2938
- By just bringing the module into scope, we can keep one level of namespacing.
2746
+ Nice! There are more things we can do with modules, including moving them into
2747
+ their own files. This is enough detail for now.
2939
2748
2940
2749
# Testing
2941
2750
@@ -3801,9 +3610,9 @@ Here's the second note, which lets us know where the first borrow would be over.
3801
3610
This is useful, because if we wait to try to borrow ` x ` after this borrow is
3802
3611
over, then everything will work.
3803
3612
3804
- These rules are very simple, but that doesn't mean that they're easy. For more
3805
- advanced patterns, please consult the [ Lifetime Guide] ( guide-lifetimes.html ) .
3806
- You'll also learn what this type signature with the ` 'a ` syntax is:
3613
+ For more advanced patterns, please consult the [ Lifetime
3614
+ Guide] ( guide-lifetimes.html ) . You'll also learn what this type signature with
3615
+ the ` 'a ` syntax is:
3807
3616
3808
3617
``` {rust,ignore}
3809
3618
pub fn as_maybe_owned(&self) -> MaybeOwned<'a> { ... }
@@ -4454,14 +4263,14 @@ for num in nums.iter() {
4454
4263
}
4455
4264
```
4456
4265
4457
- There are two reasons for this. First, this is more semantic. We iterate
4458
- through the entire vector, rather than iterating through indexes, and then
4459
- indexing the vector. Second, this version is more efficient: the first version
4460
- will have extra bounds checking because it used indexing, ` nums[i] ` . But since
4461
- we yield a reference to each element of the vector in turn with the iterator,
4462
- there's no bounds checking in the second example. This is very common with
4463
- iterators: we can ignore unnecessary bounds checks, but still know that we're
4464
- safe.
4266
+ There are two reasons for this. First, this more directly expresses what we
4267
+ mean. We iterate through the entire vector, rather than iterating through
4268
+ indexes, and then indexing the vector. Second, this version is more efficient:
4269
+ the first version will have extra bounds checking because it used indexing,
4270
+ ` nums[i] ` . But since we yield a reference to each element of the vector in turn
4271
+ with the iterator, there's no bounds checking in the second example. This is
4272
+ very common with iterators: we can ignore unnecessary bounds checks, but still
4273
+ know that we're safe.
4465
4274
4466
4275
There's another detail here that's not 100% clear because of how ` println! `
4467
4276
works. ` num ` is actually of type ` &int ` , that is, it's a reference to an ` int ` ,
0 commit comments