Rust anatomy: Difference between revisions

From wikinotes
Line 34: Line 34:
== Modules ==
== Modules ==
<blockquote>
<blockquote>
Module names govern where they can be found within the filesystem.<br>
Modules contain groups of related srcfiles.<br>
Module locations are based on where they are defined
modules are private to their parents by default, and public to their child modules.<br>
the name/namespace of the module mirrors it's filesystem location.<br>
the <code>use</code> keyword can merge a module or one of it's elements into the current namespace.


from the rust book:
See more details in [[rust modules]].
<pre>
backyard
├── Cargo.lock
├── Cargo.toml
└── src
    ├── garden
    │  └── vegetables.rs
    ├── garden.rs
    └── main.rs
</pre>
 
The module <code>foo</code>'s code could be
* <code>src/foo.rs</code>
* <code>src/foo/mod.rs</code>
</blockquote><!-- Modules -->
</blockquote><!-- Modules -->
</blockquote><!-- Project Components -->
</blockquote><!-- Project Components -->

Revision as of 23:05, 7 February 2023


Project Components

A rust project comprises of

packages build/test/share a collection of crates
crates tree of modules that build a single library, or executable
modules organize scope/privacy of paths
paths files ?

Packages

A package is a collection of crates.

  • it may have multiple executable crates
  • it may only have one library crate

Crates

A crate can be either

  • a library (has src/lib.rs entrypoint)
  • an executable (has src/main.rs entrypoint)

Modules

Modules contain groups of related srcfiles.
modules are private to their parents by default, and public to their child modules.
the name/namespace of the module mirrors it's filesystem location.
the use keyword can merge a module or one of it's elements into the current namespace.

See more details in rust modules.