You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: branches/try2/src/doc/tutorial.md
+39-34Lines changed: 39 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -2679,24 +2679,25 @@ manual.
2679
2679
2680
2680
## Files and modules
2681
2681
2682
-
One important aspect of Rust's module system is that source files and modules are not the same thing. You define a module hierarchy, populate it with all your definitions, define visibility, maybe put in a `fn main()`, and that's it.
2682
+
One important aspect about Rusts module system is that source files are not important:
2683
+
You define a module hierarchy, populate it with all your definitions, define visibility,
2684
+
maybe put in a `fn main()`, and that's it: No need to think about source files.
2683
2685
2684
-
The only file that's relevant when compiling is the one that contains the body
2685
-
of your crate root, and it's only relevant because you have to pass that file
2686
-
to `rustc` to compile your crate.
2686
+
The only file that's relevant is the one that contains the body of your crate root,
2687
+
and it's only relevant because you have to pass that file to `rustc` to compile your crate.
2687
2688
2688
-
In principle, that's all you need: You can write any Rust program as one giant source file that contains your
2689
-
crate root and everything else in `mod ... { ... }` declarations.
2689
+
And in principle, that's all you need: You can write any Rust program as one giant source file that contains your
2690
+
crate root and everything below it in `mod ... { ... }` declarations.
2690
2691
2691
-
However, in practice you usually want to split up your code into multiple
2692
-
source files to make it more manageable. Rust allows you to move the body of
2693
-
any module into its own source file. If you declare a module without its body,
2694
-
like `mod foo;`, the compiler will look for the files `foo.rs` and `foo/mod.rs`
2695
-
inside some directory (usually the same as of the source file containing the
2696
-
`mod foo;` declaration). If it finds either, it uses the content of that file
2697
-
as the body of the module. If it finds both, that's a compile error.
2692
+
However, in practice you usually want to split you code up into multiple source files to make it more manageable.
2693
+
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:
2698
2694
2699
-
To move the content of `mod farm` into its own file, you can write:
2695
+
If you declare a module without its body, like `mod foo;`, the compiler will look for the
2696
+
files `foo.rs` and `foo/mod.rs` inside some directory (usually the same as of the source file containing
2697
+
the `mod foo;`). If it finds either, it uses the content of that file as the body of the module.
2698
+
If it finds both, that's a compile error.
2699
+
2700
+
So, if we want to move the content of `mod farm` into it's own file, it would look like this:
2700
2701
2701
2702
~~~~{.ignore}
2702
2703
// `main.rs` - contains body of the crate root
@@ -2721,13 +2722,17 @@ pub mod barn {
2721
2722
2722
2723
In short, `mod foo;` is just syntactic sugar for `mod foo { /* content of <...>/foo.rs or <...>/foo/mod.rs */ }`.
2723
2724
2724
-
This also means that having two or more identical `mod foo;` declarations somewhere in your crate hierarchy is generally a bad idea,
2725
-
just like copy-and-paste-ing a module into multiple places is a bad idea.
2725
+
This also means that having two or more identical `mod foo;` somewhere
2726
+
in your crate hierarchy is generally a bad idea,
2727
+
just like copy-and-paste-ing a module into two or more places is one.
2726
2728
Both will result in duplicate and mutually incompatible definitions.
2727
2729
2728
-
When `rustc` resolves these module declarations, it starts by looking in the
2729
-
parent directory of the file containing the `mod foo` declaration. For example,
2730
-
given a file with the module body:
2730
+
The directory the compiler looks in for those two files is determined by starting with
2731
+
the same directory as the source file that contains the `mod foo;` declaration, and concatenating to that a
2732
+
path equivalent to the relative path of all nested `mod { ... }` declarations the `mod foo;`
2733
+
is contained in, if any.
2734
+
2735
+
For example, given a file with this module body:
2731
2736
2732
2737
~~~{.ignore}
2733
2738
// `src/main.rs`
@@ -2740,7 +2745,7 @@ mod animals {
2740
2745
}
2741
2746
~~~
2742
2747
2743
-
The compiler will look for these files, in this order:
Keep in mind that identical module hierarchies can still lead to different path
2757
-
lookups depending on how and where you've moved a module body to its own file.
2758
-
For example, if you move the `animals` module into its own file:
2761
+
Keep in mind that identical module hierachies can still lead to different path lookups
2762
+
depending on how and where you've moved a module body to its own file.
2763
+
For example, if we move the `animals` module above into its own file...
2759
2764
2760
2765
~~~{.ignore}
2761
2766
// `src/main.rs`
@@ -2771,21 +2776,21 @@ mod mammals {
2771
2776
}
2772
2777
~~~
2773
2778
2774
-
...then the source files of `mod animals`'s submodules can either be in the same directory as the animals source file or in a subdirectory of its directory. If the animals file is `src/animals.rs`, `rustc` will look for:
2779
+
...then the source files of `mod animals`'s submodules can
2780
+
either be placed right next to that of its parents, or in a subdirectory if `animals` source file is:
2775
2781
2776
2782
~~~{.notrust}
2777
-
src/animals.rs
2783
+
src/plants.rs
2784
+
src/plants/mod.rs
2785
+
2786
+
src/animals.rs - if file sits next to that of parent module's:
2778
2787
src/fish.rs
2779
2788
src/fish/mod.rs
2780
2789
2781
2790
src/mammals/humans.rs
2782
2791
src/mammals/humans/mod.rs
2783
-
~~
2784
-
2785
-
If the animals file is `src/animals/mod.rs`, `rustc` will look for:
2786
2792
2787
-
~~ {.notrust}
2788
-
src/animals/mod.rs
2793
+
src/animals/mod.rs - if file is in it's own subdirectory:
2789
2794
src/animals/fish.rs
2790
2795
src/animals/fish/mod.rs
2791
2796
@@ -2794,11 +2799,11 @@ src/animals/mod.rs
2794
2799
2795
2800
~~~
2796
2801
2797
-
These rules allow you to write small modules consisting of single source files which can live in the same directory as well as large modules which group submodule source files in subdirectories.
2802
+
These rules allow you to have both small modules that only need
2803
+
to consist of one source file each and can be conveniently placed right next to each other,
2804
+
and big complicated modules that group the source files of submodules in subdirectories.
2798
2805
2799
-
If you need to override where `rustc` will look for the file containing a
2800
-
module's source code, use the `path` compiler directive. For example, to load a
2801
-
`classified` module from a different file:
2806
+
If you need to circumvent the defaults, you can also overwrite the path a `mod foo;` would take:
0 commit comments