@@ -2751,197 +2751,8 @@ $ cargo run
27512751Hello, world!
27522752```
27532753
2754- Nice!
2755-
2756- There's a common pattern when you're building an executable: you build both an
2757- executable and a library, and put most of your logic in the library. That way,
2758- other programs can use that library to build their own functionality.
2759-
2760- Let's do that with our project. If you remember, libraries and executables
2761- are both crates, so while our project has one crate now, let's make a second:
2762- one for the library, and one for the executable.
2763-
2764- To make the second crate, open up ` src/lib.rs ` and put this code in it:
2765-
2766- ``` {rust}
2767- mod hello {
2768- pub fn print_hello() {
2769- println!("Hello, world!");
2770- }
2771- }
2772- ```
2773-
2774- And change your ` src/main.rs ` to look like this:
2775-
2776- ``` {rust,ignore}
2777- extern crate modules;
2778-
2779- fn main() {
2780- modules::hello::print_hello();
2781- }
2782- ```
2783-
2784- There's been a few changes. First, we moved our ` hello ` module into its own
2785- file, ` src/lib.rs ` . This is the file that Cargo expects a library crate to
2786- be named, by convention.
2787-
2788- Next, we added an ` extern crate modules ` to the top of our ` src/main.rs ` . This,
2789- as you can guess, lets Rust know that our crate relies on another, external
2790- crate. We also had to modify our call to ` print_hello ` : now that it's in
2791- another crate, we need to specify that crate first.
2792-
2793- This doesn't _ quite_ work yet. Try it:
2794-
2795- ``` {notrust,ignore}
2796- $ cargo build
2797- Compiling modules v0.0.1 (file:///home/you/projects/modules)
2798- /home/you/projects/modules/src/lib.rs:2:5: 4:6 warning: code is never used: `print_hello`, #[warn(dead_code)] on by default
2799- /home/you/projects/modules/src/lib.rs:2 pub fn print_hello() {
2800- /home/you/projects/modules/src/lib.rs:3 println!("Hello, world!");
2801- /home/you/projects/modules/src/lib.rs:4 }
2802- /home/you/projects/modules/src/main.rs:4:5: 4:32 error: function `print_hello` is private
2803- /home/you/projects/modules/src/main.rs:4 modules::hello::print_hello();
2804- ^~~~~~~~~~~~~~~~~~~~~~~~~~~
2805- error: aborting due to previous error
2806- Could not compile `modules`.
2807- ```
2808-
2809- First, we get a warning that some code is never used. Odd. Next, we get an error:
2810- ` print_hello ` is private, so we can't call it. Notice that the first error came
2811- from ` src/lib.rs ` , and the second came from ` src/main.rs ` : cargo is smart enough
2812- to build it all with one command. Also, after seeing the second error, the warning
2813- makes sense: we never actually call ` hello_world ` , because we're not allowed to!
2814-
2815- Just like modules, crates also have private visibility by default. Any modules
2816- inside of a crate can only be used by other modules in the crate, unless they
2817- use ` pub ` . In ` src/lib.rs ` , change this line:
2818-
2819- ``` {rust,ignore}
2820- mod hello {
2821- ```
2822-
2823- To this:
2824-
2825- ``` {rust,ignore}
2826- pub mod hello {
2827- ```
2828-
2829- And everything should work:
2830-
2831- ``` {notrust,ignore}
2832- $ cargo run
2833- Compiling modules v0.0.1 (file:///home/you/projects/modules)
2834- Running `target/modules`
2835- Hello, world!
2836- ```
2837-
2838- Let's do one more thing: add a ` goodbye ` module as well. Imagine a ` src/lib.rs `
2839- that looks like this:
2840-
2841- ``` {rust,ignore}
2842- pub mod hello {
2843- pub fn print_hello() {
2844- println!("Hello, world!");
2845- }
2846- }
2847-
2848- pub mod goodbye {
2849- pub fn print_goodbye() {
2850- println!("Goodbye for now!");
2851- }
2852- }
2853- ```
2854-
2855- Now, these two modules are pretty small, but imagine we've written a real, large
2856- program: they could both be huge. So maybe we want to move them into their own
2857- files. We can do that pretty easily, and there are two different conventions
2858- for doing it. Let's give each a try. First, make ` src/lib.rs ` look like this:
2859-
2860- ``` {rust,ignore}
2861- pub mod hello;
2862- pub mod goodbye;
2863- ```
2864-
2865- This tells Rust that this crate has two public modules: ` hello ` and ` goodbye ` .
2866-
2867- Next, make a ` src/hello.rs ` that contains this:
2868-
2869- ``` {rust,ignore}
2870- pub fn print_hello() {
2871- println!("Hello, world!");
2872- }
2873- ```
2874-
2875- When we include a module like this, we don't need to make the ` mod ` declaration
2876- in ` hello.rs ` , because it's already been declared in ` lib.rs ` . ` hello.rs ` just
2877- contains the body of the module which is defined (by the ` pub mod hello ` ) in
2878- ` lib.rs ` . This helps prevent 'rightward drift': when you end up indenting so
2879- many times that your code is hard to read.
2880-
2881- Finally, make a new directory, ` src/goodbye ` , and make a new file in it,
2882- ` src/goodbye/mod.rs ` :
2883-
2884- ``` {rust,ignore}
2885- pub fn print_goodbye() {
2886- println!("Bye for now!");
2887- }
2888- ```
2889-
2890- Same deal, but we can make a folder with a ` mod.rs ` instead of ` mod_name.rs ` in
2891- the same directory. If you have a lot of modules, nested folders can make
2892- sense. For example, if the ` goodbye ` module had its _ own_ modules inside of
2893- it, putting all of that in a folder helps keep our directory structure tidy.
2894- And in fact, if you place the modules in separate files, they're required to be
2895- in separate folders.
2896-
2897- This should all compile as usual:
2898-
2899- ``` {notrust,ignore}
2900- $ cargo build
2901- Compiling modules v0.0.1 (file:///home/you/projects/modules)
2902- ```
2903-
2904- We've seen how the ` :: ` operator can be used to call into modules, but when
2905- we have deep nesting like ` modules::hello::say_hello ` , it can get tedious.
2906- That's why we have the ` use ` keyword.
2907-
2908- ` use ` allows us to bring certain names into another scope. For example, here's
2909- our main program:
2910-
2911- ``` {rust,ignore}
2912- extern crate modules;
2913-
2914- fn main() {
2915- modules::hello::print_hello();
2916- }
2917- ```
2918-
2919- We could instead write this:
2920-
2921- ``` {rust,ignore}
2922- extern crate modules;
2923-
2924- use modules::hello::print_hello;
2925-
2926- fn main() {
2927- print_hello();
2928- }
2929- ```
2930-
2931- By bringing ` print_hello ` into scope, we don't need to qualify it anymore. However,
2932- it's considered proper style to do write this code like like this:
2933-
2934- ``` {rust,ignore}
2935- extern crate modules;
2936-
2937- use modules::hello;
2938-
2939- fn main() {
2940- hello::print_hello();
2941- }
2942- ```
2943-
2944- By just bringing the module into scope, we can keep one level of namespacing.
2754+ Nice! There are more things we can do with modules, including moving them into
2755+ their own files. This is enough detail for now.
29452756
29462757# Testing
29472758
0 commit comments