Questions tagged [rust-cargo]

Cargo is the official package manager for the Rust programming language.

Cargo is the official package manager for the Rust programming language. Using a manifest, the programmer can provide metadata about a project, including dependencies, which Cargo can automatically download and compile before compiling the project itself.

You can find more information in the cargo book and browse the crates (Rust packages) in crates.io.

2044 questions
72
votes
4 answers

How to get assembly output from building with Cargo?

While I've seen docs on using rustc directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious. Is there a way to run Cargo that writes out assembly files?
ideasman42
  • 42,413
  • 44
  • 197
  • 320
63
votes
1 answer

How to run a specific unit test in Rust?

I have unit tests in a package named school-info and there is a test function called repeat_students_should_not_get_full_marks. I can run all tests in the module by cargo test --package school_info. cargo test test-name will match and run tests…
not 0x12
  • 19,360
  • 22
  • 67
  • 133
60
votes
3 answers

How to include files from same directory in a module using Cargo/Rust?

I have a Cargo project consisting of three files in the same directory: main.rs, mod1.rs and mod2.rs. I want to import functions from mod2.rs to mod1.rs the same way I would import functions from mod1.rs to main.rs. I've read about the file…
Neo
  • 3,534
  • 2
  • 20
  • 32
58
votes
2 answers

How to execute cargo test using the nightly channel?

I'm trying to run my tests with nightly Rust using Windows Powershell. I run cargo test in the directory, and I get Compiling rustcraft v0.1.0 (file:///C:/Users/Phoenix/Desktop/Rust/rustcraft) error[E0554]: #![feature] may not be used on the stable…
Phoenix
  • 1,553
  • 2
  • 13
  • 29
57
votes
6 answers

Can Cargo download and build dependencies without also building the application?

Is there a way to tell Cargo to install and build all my dependencies, but not attempt to build my application? I thought cargo install would do that, but it actually goes all the way to building my app too. I want to get to a state where cargo…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
56
votes
2 answers

How to opt out of running a doc test?

I'm writing a Rust library and I want to provide examples in my documentation that compile as part of running cargo test do not run. Is this possible? I'm writing a database client library, and the examples make use of a hypothetical,…
Craig M. Brandenburg
  • 3,354
  • 5
  • 25
  • 37
55
votes
3 answers

How do I use conditional compilation with `cfg` and Cargo?

I want to conditionally compile my source code using cfg with Cargo, after Googling for a while, it seems that the solution is to use cargo --features. http://doc.crates.io/manifest.html I tried adding a few #[cfg(feature = "foo")] in the source…
盛安安
  • 1,110
  • 1
  • 8
  • 21
53
votes
5 answers

How to move tests into a separate file for binaries in Rust's Cargo?

I created a new binary using Cargo: cargo new my_binary --bin A function in my_binary/src/main.rs can be used for a test: fn function_from_main() { println!("Test OK"); } #[test] fn my_test() { function_from_main(); } And cargo test --…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
52
votes
3 answers

How do I list all of the packages I've installed globally with cargo install?

I've installed several CLI tools using cargo install (for instance, ripgrep). How do I see a list of all the crates I've downloaded using cargo install? Sort of like apt list --installed but for cargo?
almel
  • 7,178
  • 13
  • 45
  • 58
52
votes
2 answers

How can I locate resources for testing with Cargo?

I'm on a project interacting with files, and I would like to use text files to test my work. However tests aren't run from the tests/ directory, and thus I cannot reliably find them when running cargo run. Does Cargo handle this by always running…
Léo Ercolanelli
  • 1,000
  • 1
  • 8
  • 11
49
votes
1 answer

Can't use `-Z macro-backtrace` unstable option with `cargo`

I am writing rust macros and came across an error about my macro I can't understand. In hope of understanding it better, I tried to follow the compiler's advice by setting the -Z macro-backtrace unstable option and compiling again. Here is said…
Dincio
  • 1,010
  • 1
  • 13
  • 25
49
votes
2 answers

How to specify a certain commit in dependencies in Cargo.toml?

I am trying to configure my Rust project with an external dependency in GitHub. Unfortunately, some last commits made some changes in interfaces so I am unable to use the latest version. The developers also do not care of tags and separate branches…
Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
48
votes
1 answer

Error installing a crate via cargo: specified package has no binaries

I'm trying to install a Rust crate on my system (Arch Linux) using Cargo. I can search for crates and find what I need, for example: $ cargo search curl | head -n3 Updating registry `https://github.com/rust-lang/crates.io-index` curl (0.3.0) …
q9f
  • 11,293
  • 8
  • 57
  • 96
47
votes
2 answers

How to clear the Cargo cache?

When I run cargo build, various libs get stored within the folder /usr/local/lib/rustlib/. What is the correct way to clear these libs? I could rm these files manually, but would that be the right thing to do? I noticed that…
bojangle
  • 888
  • 1
  • 8
  • 14
45
votes
2 answers

How do you enable a Rust "crate feature"?

I'm trying to use rand::SmallRng. The documentation says This PRNG is feature-gated: to use, you must enable the crate feature small_rng. I've been searching and can't figure out how to enable "crate features". The phrase isn't even used anywhere…
brundolf
  • 1,170
  • 1
  • 9
  • 18