wiki/content/20200918180750-cargo.md

152 lines
2.3 KiB
Markdown
Raw Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-30 17:04:36 +00:00
date: 20200918
2024-05-06 20:40:05 +00:00
id: c409c0cd-5284-4333-ae99-bc351ff8ba0d
title: Cargo
---
# Description
Cargo is Rust's build system and package manager.
# Configuration
- [Cargo.toml](20201120094652-cargo_toml)
# Commands
## Create project
``` shell
cargo new hello_cargo
```
## Build & run project
``` shell
cargo run
```
### Backtrace
When you want to see an error backtrace set the `RUST_BACKTRACE`
environment variable:
``` shell
RUST_BACKTRACE=1 cargo run
```
## Publish to Crates.io
``` shell
cargo publish
```
## Install package
``` shell
cargo install ripgrep
```
## Linting & testing
Check code
``` shell
cargo check
```
Testing
``` shell
cargo test
```
1. Backtrace
To backtrace set the `RUST_BACKTRACE` environment variable:
``` shell
RUST_BACKTRACE=1 cargo run
```
2. Threads
By default cargo runs test in parallel. For more control over this
you can pass the number of threads you want to use. For example to
only use 1 thread:
``` shell
cargo test -- --test=threads=1
```
3. Show output for passing tests as well as failed tests
``` shell
cargo test -- --show-output
```
4. Pass test name to cargo (this equals test function name)
``` shell
cargo test one_hundred
```
5. Run ignored tests
``` shell
cargo test -- --ignored
```
Fix
The rustfix tool is included with Rust installations and can
automatically fix some compiler warnings.
``` shell
cargo fix
```
## Builds
### Profiles
In Rust, release profiles are predefined and customizable profiles with
different configurations that allow a programmer to have more control
over various options for compiling code. Each profile is configured
independently of the others.
Cargo has two main profiles: the `dev` profile Cargo uses when you run
cargo build and the release profile Cargo uses when you run
`cargo build --release`. The `dev` profile is defined with good defaults
for development, and the `release` profile has good defaults for release
builds.
1. dev
``` shell
cargo build
```
2. build
``` shell
cargo build --release
```
## Documentation
See [Rust Comments](20200827190035-rust_comments) for documentation
syntax.
### Generation
``` shell
cargo doc
```
### Open in browser
``` shell
cargo doc --open
```