Skip to content

Commit 5b6ef09

Browse files
committed
auto merge of #9398 : Kimundi/rust/work1, r=huonw
2 parents 80e0968 + bb7bc6c commit 5b6ef09

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

doc/tutorial.md

+50-13
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,8 @@ However, in practice you usually want to split you code up into multiple source
24202420
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:
24212421

24222422
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.
24242425
If it finds both, that's a compile error.
24252426

24262427
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 {
24462447
# fn main() { }
24472448
~~~~
24482449

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 */ }`.
24502451

24512452
This also means that having two or more identical `mod foo;` somewhere
24522453
in your crate hierarchy is generally a bad idea,
@@ -2455,14 +2456,14 @@ Both will result in duplicate and mutually incompatible definitions.
24552456

24562457
The directory the compiler looks in for those two files is determined by starting with
24572458
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.
24592461

24602462
For example, given a file with this module body:
24612463

24622464
~~~ {.ignore}
24632465
// src/main.rs
24642466
mod plants;
2465-
mod fungi;
24662467
mod animals {
24672468
mod fish;
24682469
mod mammals {
@@ -2477,25 +2478,61 @@ The compiler would then try all these files:
24772478
src/plants.rs
24782479
src/plants/mod.rs
24792480
2480-
src/fungi.rs
2481-
src/fungi/mod.rs
2482-
24832481
src/animals/fish.rs
24842482
src/animals/fish/mod.rs
24852483
24862484
src/animals/mammals/humans.rs
24872485
src/animals/mammals/humans/mod.rs
24882486
~~~
24892487

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.
24932530

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:
24952532

24962533
~~~ {.ignore}
2497-
#[path="../../area51/classified.rs"]
2498-
mod alien;
2534+
#[path="../../area51/alien.rs"]
2535+
mod classified;
24992536
~~~
25002537

25012538
## Importing names into the local scope

0 commit comments

Comments
 (0)