@@ -2420,7 +2420,8 @@ However, in practice you usually want to split you code up into multiple source
2420
2420
In order to do that, Rust allows you to move the body of any module into it's own source file, which works like this:
2421
2421
2422
2422
If you declare a module without its body, like ` mod foo; ` , the compiler will look for the
2423
- files ` foo.rs ` and ` foo/mod.rs ` . If it finds either, it uses the content of that file as the body of the module.
2423
+ files ` foo.rs ` and ` foo/mod.rs ` inside some directory (usually the same as of the source file containing
2424
+ the ` mod foo; ` ). If it finds either, it uses the content of that file as the body of the module.
2424
2425
If it finds both, that's a compile error.
2425
2426
2426
2427
So, if we want to move the content of ` mod farm ` into it's own file, it would look like this:
@@ -2446,7 +2447,7 @@ pub mod barn {
2446
2447
# fn main() { }
2447
2448
~~~~
2448
2449
2449
- So, in short ` mod foo; ` is just syntactic sugar for ` mod foo { /* include content of foo.rs or foo/mod.rs here */ } ` .
2450
+ In short, ` mod foo; ` is just syntactic sugar for ` mod foo { /* content of <...>/ foo.rs or <...>/ foo/mod.rs */ } ` .
2450
2451
2451
2452
This also means that having two or more identical ` mod foo; ` somewhere
2452
2453
in your crate hierarchy is generally a bad idea,
@@ -2455,14 +2456,14 @@ Both will result in duplicate and mutually incompatible definitions.
2455
2456
2456
2457
The directory the compiler looks in for those two files is determined by starting with
2457
2458
the same directory as the source file that contains the ` mod foo; ` declaration, and concatenating to that a
2458
- path equivalent to the relative path of all nested ` mod { ... } ` declarations the ` mod foo; ` is contained in, if any.
2459
+ path equivalent to the relative path of all nested ` mod { ... } ` declarations the ` mod foo; `
2460
+ is contained in, if any.
2459
2461
2460
2462
For example, given a file with this module body:
2461
2463
2462
2464
~~~ {.ignore}
2463
2465
// src/main.rs
2464
2466
mod plants;
2465
- mod fungi;
2466
2467
mod animals {
2467
2468
mod fish;
2468
2469
mod mammals {
@@ -2477,25 +2478,61 @@ The compiler would then try all these files:
2477
2478
src/plants.rs
2478
2479
src/plants/mod.rs
2479
2480
2480
- src/fungi.rs
2481
- src/fungi/mod.rs
2482
-
2483
2481
src/animals/fish.rs
2484
2482
src/animals/fish/mod.rs
2485
2483
2486
2484
src/animals/mammals/humans.rs
2487
2485
src/animals/mammals/humans/mod.rs
2488
2486
~~~
2489
2487
2490
- These rules per default result in any directory structure mirroring
2491
- the crates's module hierarchy, and allow you to have both small modules that only need
2492
- to consist of one source file, and big modules that group the source files of submodules together.
2488
+ Keep in mind that identical module hierachies can still lead to different path lookups
2489
+ depending on how and where you've moved a module body to its own file.
2490
+ For example, if we move the ` animals ` module above into its own file...
2491
+
2492
+ ~~~ {.ignore}
2493
+ // src/main.rs
2494
+ mod plants;
2495
+ mod animals;
2496
+ ~~~
2497
+ ~~~ {.ignore}
2498
+ // src/animals.rs or src/animals/mod.rs
2499
+ mod fish;
2500
+ mod mammals {
2501
+ mod humans;
2502
+ }
2503
+ ~~~
2504
+ ...then the source files of ` mod animals ` 's submodules can
2505
+ either be placed right next to that of its parents, or in a subdirectory if ` animals ` source file is:
2506
+
2507
+ ~~~ {.notrust}
2508
+ src/plants.rs
2509
+ src/plants/mod.rs
2510
+
2511
+ src/animals.rs - if file sits next to that of parent module's:
2512
+ src/fish.rs
2513
+ src/fish/mod.rs
2514
+
2515
+ src/mammals/humans.rs
2516
+ src/mammals/humans/mod.rs
2517
+
2518
+ src/animals/mod.rs - if file is in it's own subdirectory:
2519
+ src/animals/fish.rs
2520
+ src/animals/fish/mod.rs
2521
+
2522
+ src/animals/mammals/humans.rs
2523
+ src/animals/mammals/humans/mod.rs
2524
+
2525
+ ~~~
2526
+
2527
+ These rules allow you to have both small modules that only need
2528
+ to consist of one source file each and can be conveniently placed right next to each other,
2529
+ and big complicated modules that group the source files of submodules in subdirectories.
2493
2530
2494
- If you need to circumvent those defaults, you can also overwrite the path a ` mod foo; ` would take:
2531
+ If you need to circumvent the defaults, you can also overwrite the path a ` mod foo; ` would take:
2495
2532
2496
2533
~~~ {.ignore}
2497
- #[path="../../area51/classified .rs"]
2498
- mod alien ;
2534
+ #[path="../../area51/alien .rs"]
2535
+ mod classified ;
2499
2536
~~~
2500
2537
2501
2538
## Importing names into the local scope
0 commit comments