R1: Cargo Flashcards
What is Cargo?
Cargo is Rust’s build system and package manager. It can: build your code, download the libraries your code depends on, and build those libraries.
Cmd: cargo new <project name>
We can create a project. You’ll see that Cargo has generated two files and one directory for us: a Cargo.toml file and a src directory with a main.rs file inside.
Cmd: cargo new --vcs=git hello_cargo
Same as cargo new <project name>
but overrides current git config files.
What does the Cargo.toml contain?
This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.
Example:
[package]
name = “hello_cargo”
version = “0.1.0”
edition = “2021”
See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
What is [package] in Cargo.toml?
The first line, [package], is a section heading that indicates that the following statements are configuring a package.
What is [dependencies] in Cargo.toml?
Is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates.
Cmd: cargo build
We can build a project
Cmd: cargo run
We can build and run a project in one step
Cmd: cargo check
We can build a project without producing a binary to check for errors using
target/debug
directory
Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
Cmd: cargo build --release
?
When your project is finally ready for release, you can use cargo build –release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.
Cmd: cargo build --release
?
When your project is finally ready for release, you can use cargo build –release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.
module system
Rust has a number of features that allow you to manage your code’s organization, including which details are exposed, which details are private, and what names are in each scope in your programs.
These features include:
- Packages: A Cargo feature that lets you build, test, and share crates
- Crates: A tree of modules that produces a library or executable
- Modules and use: Let you control the organization, scope, and privacy of paths
- Paths: A way of naming an item, such as a struct, function, or module
crate
A crate is the smallest amount of code that the Rust compiler considers at a time. A crate can come in one of two forms:
- Binary crates: are programs you can compile to an executable that you can run, such as a command-line program or a server.
- Library crates don’t have a main function, and they don’t compile to an executable. Instead, they define functionality intended to be shared with multiple projects.
crate root
The crate root is a source file that the Rust compiler starts from and makes up the root module of your crate (usually src/lib.rs for a library crate or src/main.rs for a binary crate).