Skip to content

Commit d4daa78

Browse files
committed
Update docs for crate file changes
1 parent ebd9ad4 commit d4daa78

File tree

2 files changed

+79
-99
lines changed

2 files changed

+79
-99
lines changed

doc/rust.md

+50-70
Original file line numberDiff line numberDiff line change
@@ -570,53 +570,29 @@ Semantic rules called "dynamic semantics" govern the behavior of programs at run
570570
A program that fails to compile due to violation of a compile-time rule has no defined dynamic semantics; the compiler should halt with an error report, and produce no executable artifact.
571571

572572
The compilation model centres on artifacts called _crates_.
573-
Each compilation processes a single crate in source form, and if successful, produces a single crate in binary form: either an executable or a library.
573+
Each compilation processes a single crate in source form, and if successful, produces a single crate in binary form: either an executable or a library.^[A crate is somewhat
574+
analogous to an *assembly* in the ECMA-335 CLI model, a *library* in the
575+
SML/NJ Compilation Manager, a *unit* in the Owens and Flatt module system,
576+
or a *configuration* in Mesa.]
574577

575578
A _crate_ is a unit of compilation and linking, as well as versioning, distribution and runtime loading.
576579
A crate contains a _tree_ of nested [module](#modules) scopes.
577580
The top level of this tree is a module that is anonymous (from the point of view of paths within the module) and any item within a crate has a canonical [module path](#paths) denoting its location within the crate's module tree.
578581

579-
Crates are provided to the Rust compiler through two kinds of file:
580-
581-
- _crate files_, that end in `.rc` and each define a `crate`.
582-
- _source files_, that end in `.rs` and each define a `module`.
583-
584-
> **Note:** The functionality of crate files will be merged into source files in future versions of Rust.
585-
> The separate processing of crate files, both their grammar and file extension, will be removed.
586-
587-
The Rust compiler is always invoked with a single crate file as input, and always produces a single output crate.
588-
589-
When the Rust compiler is invoked with a crate file, it reads the _explicit_
590-
definition of the crate it's compiling from that file, and populates the
591-
crate with modules derived from all the source files referenced by the
592-
crate, reading and processing all the referenced modules at once.
593-
594-
When the Rust compiler is invoked with a source file, it creates an _implicit_ crate and treats the source file as if it is the sole module populating this explicit crate.
595-
The module name is derived from the source file name, with the `.rs` extension removed.
596-
597-
## Crate files
582+
The Rust compiler is always invoked with a single source file as input, and always produces a single output crate.
583+
The processing of that source file may result in other source files being loaded as modules.
584+
Source files typically have the extension `.rs` but, by convention,
585+
source files that represent crates have the extension `.rc`, called *crate files*.
598586

599-
~~~~~~~~ {.ebnf .gram}
600-
crate : attribute [ ';' | attribute* directive ]
601-
| directive ;
602-
directive : view_item | dir_directive | source_directive ;
603-
~~~~~~~~
604-
605-
A crate file contains a crate definition, for which the production above
606-
defines the grammar. It is a declarative grammar that guides the compiler in
607-
assembling a crate from component source files.^[A crate is somewhat
608-
analogous to an *assembly* in the ECMA-335 CLI model, a *library* in the
609-
SML/NJ Compilation Manager, a *unit* in the Owens and Flatt module system,
610-
or a *configuration* in Mesa.] A crate file describes:
611-
612-
* [Attributes](#attributes) about the crate, such as author, name, version,
613-
and copyright. These are used for linking, versioning and distributing
614-
crates.
615-
* The source-file and directory modules that make up the crate.
616-
* Any `use` or `extern mod` [view items](#view-items) that apply to
617-
the anonymous module at the top-level of the crate's module tree.
587+
A Rust source file describes a module, the name and
588+
location of which -- in the module tree of the current crate -- are defined
589+
from outside the source file: either by an explicit `mod_item` in
590+
a referencing source file, or by the name of the crate ittself.
618591

619-
An example of a crate file:
592+
Each source file contains a sequence of zero or more `item` definitions,
593+
and may optionally begin with any number of `attributes` that apply to the containing module.
594+
Atributes on the anonymous crate module define important metadata that influences
595+
the behavior of the compiler.
620596

621597
~~~~~~~~{.xfail-test}
622598
// Linkage attributes
@@ -629,40 +605,17 @@ An example of a crate file:
629605
license = "BSD" ];
630606
author = "Jane Doe" ];
631607
632-
// Import a module.
633-
extern mod std (ver = "1.0");
608+
// Specify the output type
609+
#[ crate_type = "lib" ];
634610
635-
// Define some modules.
636-
#[path = "foo.rs"]
637-
mod foo;
638-
mod bar {
639-
#[path = "quux.rs"]
640-
mod quux;
641-
}
611+
// Turn on a warning
612+
#[ warn(non_camel_case_types) ];
642613
~~~~~~~~
643614

644-
### Dir directives
645-
646-
A `dir_directive` forms a module in the module tree making up the crate, as
647-
well as implicitly relating that module to a directory in the filesystem
648-
containing source files and/or further subdirectories. The filesystem
649-
directory associated with a `dir_directive` module can either be explicit,
650-
or if omitted, is implicitly the same name as the module.
651-
652-
A `source_directive` references a source file, either explicitly or implicitly, by combining the module name with the file extension `.rs`.
653-
The module contained in that source file is bound to the module path formed by the `dir_directive` modules containing the `source_directive`.
654-
655-
## Source files
656-
657-
A source file contains a `module`: that is, a sequence of zero or more
658-
`item` definitions. Each source file is an implicit module, the name and
659-
location of which -- in the module tree of the current crate -- is defined
660-
from outside the source file: either by an explicit `source_directive` in
661-
a referencing crate file, or by the filename of the source file itself.
662-
663-
A source file that contains a `main` function can be compiled to an executable.
615+
A crate that contains a `main` function can be compiled to an executable.
664616
If a `main` function is present, its return type must be [`unit`](#primitive-types) and it must take no arguments.
665617

618+
666619
# Items and attributes
667620

668621
Crates contain [items](#items),
@@ -719,7 +672,7 @@ That is, Rust has no notion of type abstraction: there are no first-class "foral
719672
### Modules
720673

721674
~~~~~~~~ {.ebnf .gram}
722-
mod_item : "mod" ident '{' mod '}' ;
675+
mod_item : "mod" ident ( ';' | '{' mod '}' );
723676
mod : [ view_item | item ] * ;
724677
~~~~~~~~
725678

@@ -757,6 +710,33 @@ Declaring a named type that has the same name as a module in scope is forbidden:
757710
that is, a type definition, trait, struct, enumeration, or type parameter
758711
can't shadow the name of a module in scope, or vice versa.
759712

713+
A module without a body is loaded from an external file, by default with the same
714+
name as the module, plus the `.rs` extension.
715+
When a nested submodule is loaded from an external file,
716+
it is loaded from a subdirectory path that mirrors the module hierarchy.
717+
718+
~~~ {.xfail-test}
719+
// Load the `vec` module from `vec.rs`
720+
mod vec;
721+
722+
mod task {
723+
// Load the `local_data` module from `task/local_data.rs`
724+
mod local_data;
725+
}
726+
~~~
727+
728+
The directories and files used for loading external file modules can be influenced
729+
with the `path` attribute.
730+
731+
~~~ {.xfail-test}
732+
#[path = "task_files"]
733+
mod task {
734+
// Load the `local_data` module from `task_files/tls.rs`
735+
#[path = "tls.rs"]
736+
mod local_data;
737+
}
738+
~~~
739+
760740
#### View items
761741

762742
~~~~~~~~ {.ebnf .gram}

doc/tutorial.md

+29-29
Original file line numberDiff line numberDiff line change
@@ -2252,22 +2252,46 @@ The unit of independent compilation in Rust is the crate: rustc
22522252
compiles a single crate at a time, from which it produces either a
22532253
library or executable.
22542254

2255-
When compiling a single `.rs` file, the file acts as the whole crate.
2255+
When compiling a single `.rs` source file, the file acts as the whole crate.
22562256
You can compile it with the `--lib` compiler switch to create a shared
22572257
library, or without, provided that your file contains a `fn main`
22582258
somewhere, to create an executable.
22592259

2260-
Larger crates typically span multiple files and are compiled from
2261-
a crate (.rc) file. Crate files contain their own syntax for loading
2262-
modules from .rs files and typically include metadata about the crate.
2260+
Larger crates typically span multiple files and are, by convention,
2261+
compiled from a source file with the `.rc` extension, called a *crate file*.
2262+
The crate file extension distinguishes source files that represent
2263+
crates from those that do not, but otherwise source files and crate files are identical.
2264+
2265+
A typical crate file declares attributes associated with the crate that
2266+
may affect how the compiler processes the source.
2267+
Crate attributes specify metadata used for locating and linking crates,
2268+
the type of crate (library or executable),
2269+
and control warning and error behavior,
2270+
among other things.
2271+
Crate files additionally declare the external crates they depend on
2272+
as well as any modules loaded from other files.
22632273

22642274
~~~~ { .xfail-test }
2275+
// Crate linkage metadata
22652276
#[link(name = "farm", vers = "2.5", author = "mjh")];
2277+
2278+
// Make a library ("bin" is the default)
22662279
#[crate_type = "lib"];
22672280
2281+
// Turn on a warning
2282+
#[warn(non_camel_case_types)]
2283+
2284+
// Link to the standard library
2285+
extern mod std;
2286+
2287+
// Load some modules from other files
22682288
mod cow;
22692289
mod chicken;
22702290
mod horse;
2291+
2292+
fn main() {
2293+
...
2294+
}
22712295
~~~~
22722296

22732297
Compiling this file will cause `rustc` to look for files named
@@ -2282,7 +2306,7 @@ module, which other crates can use to load the right module. More
22822306
about that later.
22832307

22842308
To have a nested directory structure for your source files, you can
2285-
nest mods in your `.rc` file:
2309+
nest mods:
22862310

22872311
~~~~ {.ignore}
22882312
mod poultry {
@@ -2296,30 +2320,6 @@ The compiler will now look for `poultry/chicken.rs` and
22962320
and `poultry::turkey`. You can also provide a `poultry.rs` to add
22972321
content to the `poultry` module itself.
22982322

2299-
When compiling .rc files, if rustc finds a .rs file with the same
2300-
name, then that .rs file provides the top-level content of the crate.
2301-
2302-
~~~ {.xfail-test}
2303-
// foo.rc
2304-
#[link(name = "foo", vers="1.0")];
2305-
2306-
mod bar;
2307-
~~~
2308-
2309-
~~~ {.xfail-test}
2310-
// foo.rs
2311-
fn main() { bar::baz(); }
2312-
~~~
2313-
2314-
> ***Note***: The way rustc looks for .rs files to pair with .rc
2315-
> files is a major source of confusion and will change. It's likely
2316-
> that the crate and source file grammars will merge.
2317-
2318-
> ***Note***: The way that directory modules are handled will also
2319-
> change. The code for directory modules currently lives in a .rs
2320-
> file with the same name as the directory, _next to_ the directory.
2321-
> A new scheme will make that file live _inside_ the directory.
2322-
23232323
## Using other crates
23242324

23252325
The `extern mod` directive lets you use a crate (once it's been

0 commit comments

Comments
 (0)