BertKing
BertKing

Reputation: 529

How to better understand what a "crate" is in Rust?

In this book- Rust By Example,Chapter 11:

A crate is a compilation unit in Rust. Whenever rustc some_file.rs is called, some_file.rs is treated as the crate file.

According to this book, what about the source file ?

The Rust Reference | Crates and source files

The compilation model centers on artifacts called crates. Each compilation processes a single crate in source form, and if successful, produces a single crate in binary form: either an executable or some sort of library.

The Rust compiler is always invoked with a single source file as input, and always produces a single output crate. The processing of that source file may result in other source files being loaded as modules. Source files have the extension .rs.

According to this statement, I think:

Source file(.rs file) --> corresponding crate

Just like : .java --> .class

Now I can't understand this problem; I'm all at sea.

Upvotes: 6

Views: 709

Answers (1)

Kevin Reid
Kevin Reid

Reputation: 43982

This is the key part of the material you've quoted:

The processing of that source file may result in other source files being loaded as modules.

If you examine a typical library, you will find a file called src/lib.rs which contains several lines like mod foo;. Each of those identifies another file src/foo.rs which the compiler will interpret as another module making up part of the crate (or it can contain the module directly, in the same file).

It is not that one source file makes up a crate: it's that starting from that one source file, you can find all the files making up the crate, as opposed to other compilation models where the compiler might be given many file names to start from.

Upvotes: 8

Related Questions