Rust anatomy: Difference between revisions
From wikinotes
No edit summary |
|||
(17 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This page is a general getting started in rust. | |||
= Filesystem = | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
my_project/ | |||
src/ | |||
my_submodule/ | |||
mod.rs # my_submodule's code | |||
foobar.rs # my_submodule::foobar's code | |||
main.rs # executable entrypoint && tests | |||
lib.rs # library entrypoint && tests | |||
tests/ | |||
*.rs # integration tests | |||
Cargo.toml | |||
Cargo.lock | |||
</syntaxhighlight> | |||
</blockquote><!-- Filesystem --> | |||
= Examples = | |||
<blockquote> | |||
== Commandline Executable == | |||
<blockquote> | |||
TODO | |||
</blockquote><!-- Commandline Executable --> | |||
</blockquote><!-- Examples --> | |||
= Project Components = | |||
<blockquote> | |||
A rust project comprises of | A rust project comprises of | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |||
| workspaces || (optional) a set of packages sharing a Cargo.toml | |||
|- | |- | ||
| packages || build/test/share a collection of crates | | packages || build/test/share a collection of crates | ||
Line 10: | Line 40: | ||
| modules || organize scope/privacy of paths | | modules || organize scope/privacy of paths | ||
|- | |- | ||
| paths || files | | paths || rust files | ||
|} | |} | ||
<syntaxhighlight lang=" | == Packages == | ||
package | <blockquote> | ||
A package is a collection of crates. | |||
* it may have multiple executable crates | |||
* it may only have one library crate | |||
<syntaxhighlight lang="bash"> | |||
myproject/ | |||
src/ | |||
Cargo.toml | |||
Cargo.lock | |||
</syntaxhighlight> | |||
{{ expand | |||
| <code>Cargo.toml</code> | |||
| | |||
<syntaxhighlight lang="toml"> | |||
[package] | |||
name = "my_project" | |||
version = "0.1.0" | |||
authors = ["Your Name <you@example.com>"] | |||
edition = "2018" | |||
[dependencies] | |||
time = "0.1.12" | |||
regex = "0.1.41" | |||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | |||
<syntaxhighlight lang="bash"> | |||
cargo new my_project # generate a new project | |||
</syntaxhighlight> | |||
</blockquote><!-- Packages --> | </blockquote><!-- Packages --> | ||
== Crates == | == Crates == | ||
<blockquote> | <blockquote> | ||
A crate is a build target for rust.<br> | |||
It can be either a library or an executable. | |||
If a package only has a single library/executable, the crate type can be implied from the source files. | |||
<syntaxhighlight lang="bash"> | |||
src/lib.rs # if this exists, crate is a library | |||
src/main.rs # if this exists, crate is an executable | |||
</syntaxhighlight> | |||
You can also specify multiple crates for your package in <code>Cargo.toml</code>. | |||
<syntaxhighlight lang="bash"> | |||
# Cargo.toml | |||
[package] | |||
# ... | |||
[[bin]] | |||
name = "foo" # refers to src/bin/foo.rs | |||
[[bin]] | |||
name = "bar" # refers to src/bin/bar.rs | |||
[lib] # <-- single '['s | |||
# refers to 'src/lib.rs', unless 'path' overrides it | |||
</syntaxhighlight> | |||
</blockquote><!-- Crates --> | </blockquote><!-- Crates --> | ||
</blockquote><!-- Components --> | |||
== Modules == | |||
<blockquote> | |||
Modules contain groups of related srcfiles.<br> | |||
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. | |||
See more details in [[rust modules]]. | |||
</blockquote><!-- Modules --> | |||
</blockquote><!-- Project Components --> |
Latest revision as of 21:41, 9 February 2023
This page is a general getting started in rust.
Filesystem
my_project/ src/ my_submodule/ mod.rs # my_submodule's code foobar.rs # my_submodule::foobar's code main.rs # executable entrypoint && tests lib.rs # library entrypoint && tests tests/ *.rs # integration tests Cargo.toml Cargo.lock
Examples
Commandline Executable
TODO
Project Components
A rust project comprises of
workspaces (optional) a set of packages sharing a Cargo.toml 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 rust files Packages
A package is a collection of crates.
- it may have multiple executable crates
- it may only have one library crate
myproject/ src/ Cargo.toml Cargo.lock
Cargo.toml
[package] name = "my_project" version = "0.1.0" authors = ["Your Name <you@example.com>"] edition = "2018" [dependencies] time = "0.1.12" regex = "0.1.41"
cargo new my_project # generate a new projectCrates
A crate is a build target for rust.
It can be either a library or an executable.If a package only has a single library/executable, the crate type can be implied from the source files.
src/lib.rs # if this exists, crate is a library src/main.rs # if this exists, crate is an executableYou can also specify multiple crates for your package in
Cargo.toml
.# Cargo.toml [package] # ... [[bin]] name = "foo" # refers to src/bin/foo.rs [[bin]] name = "bar" # refers to src/bin/bar.rs [lib] # <-- single '['s # refers to 'src/lib.rs', unless 'path' overrides itModules
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.
theuse
keyword can merge a module or one of it's elements into the current namespace.See more details in rust modules.