Skip to content

Instantly share code, notes, and snippets.

@ariady-putra
Last active April 30, 2024 10:39
Show Gist options
  • Select an option

  • Save ariady-putra/3036345f2641827723f49e096be034d2 to your computer and use it in GitHub Desktop.

Select an option

Save ariady-putra/3036345f2641827723f49e096be034d2 to your computer and use it in GitHub Desktop.
Rust quick reference

How to update rustc

Run rustup commands:

rustup update
rustup update stable
rustup update nightly

Select rustc version:

rustup default stable
rustup default nightly

and then run rustc --version to verify.

Cargo quick reference

cargo --version
cargo new project_name

GIT files won’t be generated if you run cargo new within an existing GIT repository; you can override this behavior by using cargo new --vcs=[git|hg|pijul|fossil|none]

NOTE: git is a common version control system; you can change cargo new to use a different version control system or no version-control-system by using:

cargo new project_name --vcs=none

cargo new and cargo init are both commands provided by the Cargo package manager in Rust, but they serve slightly different purposes.

  1. cargo new: The cargo new command is used to create a new Rust project from scratch. When you run cargo new project_name, Cargo creates a new directory with the specified project_name and initializes it as a new Rust project. It generates the necessary files and directories, including a default Cargo.toml file and a src/main.rs file, which serves as the entry point for the project. This command is typically used when starting a new project and want Cargo to set up the initial project structure for you.

  2. cargo init: The cargo init command is used to initialize a Rust project in an existing directory. If you navigate to an empty directory or a directory that doesn't have a Cargo.toml file, running cargo init will create a new Cargo.toml file and set up the necessary project configuration. It does not create any additional files or directories by default. This command is useful when you want to convert an existing directory into a Rust project or when you want to manually configure the project structure.

In summary, cargo new is used to create a new Rust project from scratch, setting up the initial project structure and files. On the other hand, cargo init initializes a Rust project in an existing directory by creating the Cargo.toml file and configuring the project, without generating additional files or directories.

Choose the appropriate command based on whether you want to start a new project or initialize an existing directory as a Rust project.

How to add a dependency

cargo add crate_name

How to update ALL dependencies

cargo update

How to update a specific dependency

cargo update -p crate_name

More commands

cargo check
cargo build
cargo run

cargo check --release
cargo build --release
cargo run --release

cargo clean
cargo clean --release

cargo doc
cargo doc --open
cargo doc --release
cargo doc --release --open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment