Rust
Cargo Project Commands
-
cargo init: Initialize the current working directory as a Cargo project. -
cargo new <path>: Create dir<path>and initialize it as a Cargo project. -
cargo build: Build the project specified byCargo.tomltotarget/.-
Builds to
target/debugby default. Specify--releaseto build totarget/release.
-
-
cargo run: Build the project and run themain()entrypoint in one command. -
cargo clean: Clean up any built targets and thetarget/dir. -
cargo check: Checks your code to make sure it compiles but doesn’t produce an executable. -
cargo doc --open: Generates and opens documentation for the dependencies you’ve installed via Cargo.
Enums in Rust
Defining an enum:
enum IpAddrKind {
V4,
V6,
}
// Example usage:
fn main() {
let four: IpAddrKind = IpAddrKind::V4;
let four: IpAddrKind = IpAddrKind::V6;
}
Enums can hold values within them, by defining enums with parentheses after each variant.
enum IpAddrKind {
V4(String),
V6(String),
}
// Example usage:
fn main() {
let four: IpAddrKind = IpAddrKind::V4("127.0.0.1");
let four: IpAddrKind = IpAddrKind::V6("00:00:00:00:00:00");
}