Skip to content

Cargo

To build

sh
cargo build

To run

sh
cargo run

To run an example

sh
cargo run --exmaple <name>

To install new crates from crates.io

sh
cargo install <crate_name>

This one is a nice crate.

This extends Cargo to allow you to add and remove dependencies by modifying your Cargo.toml

sh
cargo install cargo-edit

Using cargo-edit crate

  1. Search
sh
cargo search <crate>
  1. Add
sh
cargo add <crate>
  1. Add with feature
sh
cargo add <crate> -F <feature>

To format rust code in a specific style Add rustfmt.toml right next to Cargo.toml

Modify to have braces in next line

sh
brace_style = "AlwaysNextLine"
control_brace_style = "AlwaysNextLine"

You need to set toolchain to nightly to work with this

sh
rustup override set nightly

Run this to format your code

sh
cargo fmt

To publish on crates.io

sh
cargo login
cargo publish --dry-run
cargo publish