@@ -126,15 +126,15 @@ ways.
126
126
127
127
Instead of declaring a module like this:
128
128
129
- ``` { rust,ignore}
129
+ ``` rust,ignore
130
130
mod english {
131
131
// contents of our module go here
132
132
}
133
133
```
134
134
135
135
We can instead declare our module like this:
136
136
137
- ``` { rust,ignore}
137
+ ``` rust,ignore
138
138
mod english;
139
139
```
140
140
@@ -173,7 +173,7 @@ $ tree .
173
173
174
174
` src/lib.rs ` is our crate root, and looks like this:
175
175
176
- ``` { rust,ignore}
176
+ ``` rust,ignore
177
177
mod english;
178
178
mod japanese;
179
179
```
@@ -184,7 +184,7 @@ on our preference. In this case, because our modules have sub-modules, we’ve
184
184
chosen the second. Both ` src/english/mod.rs ` and ` src/japanese/mod.rs ` look
185
185
like this:
186
186
187
- ``` { rust,ignore}
187
+ ``` rust,ignore
188
188
mod greetings;
189
189
mod farewells;
190
190
```
@@ -297,7 +297,7 @@ public, and so private is the default. To make things public, you use the `pub`
297
297
keyword. Let’s focus on the ` english` module first, so let’s reduce our ` src/main.rs`
298
298
to just this:
299
299
300
- ` ` ` { rust,ignore}
300
+ ` ` ` rust,ignore
301
301
extern crate phrases;
302
302
303
303
fn main () {
@@ -308,29 +308,29 @@ fn main() {
308
308
309
309
In our ` src/lib.rs` , let’s add ` pub` to the ` english` module declaration:
310
310
311
- ` ` ` { rust,ignore}
311
+ ` ` ` rust,ignore
312
312
pub mod english;
313
313
mod japanese;
314
314
` ` `
315
315
316
316
And in our ` src/english/mod.rs` , let’s make both ` pub` :
317
317
318
- ` ` ` { rust,ignore}
318
+ ` ` ` rust,ignore
319
319
pub mod greetings;
320
320
pub mod farewells;
321
321
` ` `
322
322
323
323
In our ` src/english/greetings.rs` , let’s add ` pub` to our ` fn` declaration:
324
324
325
- ` ` ` { rust,ignore}
325
+ ` ` ` rust,ignore
326
326
pub fn hello () -> String {
327
327
" Hello!" .to_string()
328
328
}
329
329
` ` `
330
330
331
331
And also in ` src/english/farewells.rs` :
332
332
333
- ` ` ` { rust,ignore}
333
+ ` ` ` rust,ignore
334
334
pub fn goodbye () -> String {
335
335
" Goodbye." .to_string()
336
336
}
@@ -365,7 +365,7 @@ refer to them with shorter names. Let’s talk about `use`.
365
365
Rust has a ` use` keyword, which allows us to import names into our local scope.
366
366
Let’s change our ` src/main.rs` to look like this:
367
367
368
- ` ` ` { rust,ignore}
368
+ ` ` ` rust,ignore
369
369
extern crate phrases;
370
370
371
371
use phrases::english::greetings;
@@ -382,7 +382,7 @@ the functions by a much shorter name. By convention, when importing functions, i
382
382
considered best practice to import the module, rather than the function directly. In
383
383
other words, you _can_ do this:
384
384
385
- ` ` ` { rust,ignore}
385
+ ` ` ` rust,ignore
386
386
extern crate phrases;
387
387
388
388
use phrases::english::greetings::hello;
@@ -400,7 +400,7 @@ becomes a problem. If we have conflicting names, Rust will give a compilation
400
400
error. For example, if we made the ` japanese` functions public, and tried to do
401
401
this:
402
402
403
- ` ` ` { rust,ignore}
403
+ ` ` ` rust,ignore
404
404
extern crate phrases;
405
405
406
406
use phrases::english::greetings::hello;
@@ -426,14 +426,14 @@ Could not compile `phrases`.
426
426
If we’re importing multiple names from the same module, we don’t have to type it out
427
427
twice. Instead of this:
428
428
429
- ` ` ` { rust,ignore}
429
+ ` ` ` rust,ignore
430
430
use phrases::english::greetings;
431
431
use phrases::english::farewells;
432
432
` ` `
433
433
434
434
We can use this shortcut:
435
435
436
- ` ` ` { rust,ignore}
436
+ ` ` ` rust,ignore
437
437
use phrases::english::{greetings, farewells};
438
438
` ` `
439
439
@@ -445,7 +445,7 @@ interface that may not directly map to your internal code organization.
445
445
446
446
Let’s look at an example. Modify your ` src/main.rs` to read like this:
447
447
448
- ` ` ` { rust,ignore}
448
+ ` ` ` rust,ignore
449
449
extern crate phrases;
450
450
451
451
use phrases::english::{greetings,farewells};
@@ -462,30 +462,30 @@ fn main() {
462
462
463
463
Then, modify your ` src/lib.rs` to make the ` japanese` mod public:
464
464
465
- ` ` ` { rust,ignore}
465
+ ` ` ` rust,ignore
466
466
pub mod english;
467
467
pub mod japanese;
468
468
` ` `
469
469
470
470
Next, make the two functions public, first in ` src/japanese/greetings.rs` :
471
471
472
- ` ` ` { rust,ignore}
472
+ ` ` ` rust,ignore
473
473
pub fn hello () -> String {
474
474
" こんにちは" .to_string()
475
475
}
476
476
` ` `
477
477
478
478
And then in ` src/japanese/farewells.rs` :
479
479
480
- ` ` ` { rust,ignore}
480
+ ` ` ` rust,ignore
481
481
pub fn goodbye () -> String {
482
482
" さようなら" .to_string()
483
483
}
484
484
` ` `
485
485
486
486
Finally, modify your ` src/japanese/mod.rs` to read like this:
487
487
488
- ` ` ` { rust,ignore}
488
+ ` ` ` rust,ignore
489
489
pub use self::greetings::hello;
490
490
pub use self::farewells::goodbye;
491
491
0 commit comments