Questions tagged [rust-cargo]
Cargo is the official package manager for the Rust programming language.
rust-cargo
2,352
questions
410
votes
4
answers
156k
views
Package with both a library and a binary?
I would like to make a Rust package that contains both a reusable library (where most of the program is implemented), and also an executable that uses it.
Assuming I have not confused any semantics ...
368
votes
7
answers
106k
views
Why are Rust executables so huge?
I find the approach and the way they define the language in the first two chapters of the documentation particularly interesting. So I decided to get my fingers wet and started out with "Hello, ...
287
votes
24
answers
160k
views
Cargo build hangs with " Blocking waiting for file lock on the registry index" after building parity from source
I followed the readme instructions for building Parity from source and then executed:
cargo build --release
~/.cargo/bin/cargo build --release
as instructed, both of which returned the following ...
284
votes
5
answers
59k
views
How can a Rust program access metadata from its Cargo package?
How do you access a Cargo package's metadata (e.g. version) from the Rust code in the package? In my case, I am building a command line tool that I'd like to have a standard --version flag, and I'd ...
260
votes
3
answers
114k
views
How can I build multiple binaries with Cargo?
I'd like to make a project with a daemon and a client, connecting through a unix socket.
A client and a daemon requires two binaries, so how do I tell Cargo to build two targets from two different ...
250
votes
5
answers
186k
views
Is there a command to update Cargo to the latest official release?
I seem to have diverging versions of rustc and cargo (I think),
$ rustc -V
rustc 1.9.0 (e4e8b6668 2016-05-18)
$ cargo -V
cargo 0.10.0-nightly (10ddd7d 2016-04-08)
Is there a command akin to
pip ...
168
votes
5
answers
60k
views
Does cargo install have an equivalent update command?
I used cargo install to globally install a package, such as rustfmt or racer.
How can I update the installed package without first deleting it ( cargo uninstall) and then running cargo install again.
...
163
votes
1
answer
77k
views
How to check release / debug builds using cfg in Rust?
With the C pre-processor it's common to do,
#if defined(NDEBUG)
// release build
#endif
#if defined(DEBUG)
// debug build
#endif
Cargo's rough equivalents are:
cargo build --release for ...
128
votes
1
answer
51k
views
What is a crate attribute and where do I add it?
In order to get a feel for how Rust works, I decided to look at a little terminal-based text editor called Iota. I cloned the repository and ran cargo build only to be told:
error: *if let* syntax ...
118
votes
1
answer
21k
views
How to define test-only dependencies?
I have a Rust library that implements a lint plugin. I want to include compiletest, but not require it outside of testing. What is the correct way to specify that the dependency is for testing only?
109
votes
6
answers
73k
views
error[E0554]: #![feature] may not be used on the stable release channel Couldn't install racer using cargo
I'm trying to install racer using cargo, so I executed the command cargo install racer in the terminal and it resulted in the error:
error[E0554]: #![feature] may not be used on the stable release ...
100
votes
2
answers
49k
views
How to get a release build with debugging information when using cargo?
The following command
$ cargo build
produces a non-optimized build with debugging information. On the contrary,
$ cargo build --release
produces an optimized build without debugging information.
...
95
votes
3
answers
30k
views
How can I specify binary-only dependencies?
I have a crate with both a binary and a library. The library is extremely light on dependencies, while the binary requires quite a bit more to, e.g., load files or do scoped parallel things.
...
95
votes
8
answers
36k
views
How can I get cargo to recompile changed files automatically?
I have heard cargo has the ability to automatically recompile changed source files, but I'm having a hard time figuring out how to tell it to do so.
For now, I am manually running cargo build or ...
83
votes
5
answers
72k
views
How to launch a Rust application from Visual Studio Code?
I have installed the Visual Studio Code extensions for Rust:
I want to run my project and I don't understand where to click.
I tried clicking Run Task, Run build task, Configure Default build task, ...
82
votes
4
answers
41k
views
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 ...
81
votes
1
answer
53k
views
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.
...
77
votes
4
answers
35k
views
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?...
73
votes
3
answers
18k
views
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 ...
66
votes
2
answers
80k
views
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 ...
65
votes
3
answers
39k
views
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 ...
64
votes
2
answers
60k
views
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[...
61
votes
4
answers
49k
views
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 ...
60
votes
6
answers
26k
views
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 ...
58
votes
5
answers
40k
views
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() {...
57
votes
2
answers
28k
views
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 "...
56
votes
2
answers
19k
views
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 ...
56
votes
6
answers
20k
views
How to tell what "features" are available per crate?
Is there a standard way to determine what features are available for a given crate?
I'm trying to read Postgres timezones, and this says to use the crate postgres = "0.17.0-alpha.1" crate's with-time ...
55
votes
2
answers
25k
views
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 ...
50
votes
1
answer
12k
views
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 ...
49
votes
2
answers
35k
views
Cannot find tokio::main macro?
I am creating a sample Rust project in my Windows system to download a file by HTTP GET request in async mode.
My code is as follows (same as the code mentioned in the Rust Cookbook):
extern crate ...
49
votes
2
answers
54k
views
How to pass rustc flags to cargo?
I am trying to disable dead code warnings. I tried the following
cargo build -- -A dead_code
➜ rla git:(master) ✗ cargo build -- -A dead_code
error: Invalid arguments.
So I am wondering how ...
49
votes
2
answers
16k
views
How do I debug a failing cargo test in GDB?
I have a failing Cargo test:
$ cargo test
[snip]
Running target/gunzip-c62d8688496249d8
running 2 tests
test test_extract_failure ... FAILED
test test_extract_success ... ok
failures:
---- ...
48
votes
7
answers
34k
views
How to run cargo fmt on save in vscode?
Is it possible to make Visual Studio Code run cargo fmt on file save?
48
votes
7
answers
42k
views
failed to parse manifest - no targets specified
I am new to Rust and attempting to build a test project with Cargo. My Cargo.toml looks like:
[package]
name = "rust-play"
version = "0.0.1"
authors = [ "Bradley Wogsland <omitted>" ]
(but the ...
48
votes
1
answer
22k
views
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://...
46
votes
3
answers
59k
views
How can I set default build target for Cargo?
I tried to make 'Hello World' in Rust using this tutorial, but the build command is a bit verbose:
cargo +nightly build --target wasm32-unknown-unknown --release
Is it possible to set the default ...
46
votes
1
answer
55k
views
How do I set environment variables with Cargo?
I have a dependency listed in Cargo.toml that needs a specific environment variable set. I can run export FOO=bar in bash and all works well, but for the life of me I can't figure out how to export ...
46
votes
2
answers
18k
views
Can tests be built in release mode using Cargo?
I'm using cargo build --release to build my project in release configuration and cargo test to build and run my tests.
However, I'd like to also build my tests in release mode; can this be done using ...
44
votes
5
answers
46k
views
How do I use external crates in Rust?
I'm trying to work with the rust-http library, and I'd like to use it as the basis for a small project.
I have no idea how to use something that I can't install via rustpkg install <remote_url>....
43
votes
4
answers
29k
views
How do I tell Cargo to build files other than main.rs?
Here is my directory structure:
lowks@lowkster ~/src/rustlang/gettingrusty $ tree .
.
├── Cargo.lock
├── Cargo.toml
├── foo.txt
├── src
│ ├── boolean_example.rs
│ ├── function_goodbye_world.rs
│ ...
41
votes
3
answers
32k
views
How do I change the default rustc / Cargo linker?
I would like to make rustc use lld as a linker instead of ld in a particular crate. So I create .cargo/config in my project directory with the following:
[target.x86_64-unknown-linux-gnu] ...
40
votes
1
answer
8k
views
Is `cargo clippy` a superset of `cargo check`?
I'm trying to build and test my Rust code with a CI, and I'm wondering whether cargo clippy (potentially with options) covers everything that cargo check does. Do I only need to run cargo clippy, or ...
39
votes
3
answers
25k
views
Is there a list of all cfg features?
Rust has the ability to check configuration at build time with, e.g., #[cfg(target_os = "linux")] or if cfg!(target_os = "linux") {...}, where target_os is a feature.
Is there a list of all (or, at ...
38
votes
2
answers
71k
views
How to pass RUST_BACKTRACE=1 when running a Rust binary installed in Debian?
When I run a binary using cargo, I have the option to run it as follows -
bash -c "RUST_BACKTRACE=1 cargo run --bin my_binary"
This gives me a stack trace when the binary hits an error.
But when I ...
37
votes
7
answers
46k
views
rustup gives command not found error with zsh even after installing with brew
I installed rustup with brew command but still says that rustup command is not found. I am not sure if I am missing any installation step. Any help would be appreciated.
37
votes
3
answers
9k
views
What is an idiomatic way to have shared utility functions for integration tests and benchmarks?
I have Rust project with both integration tests (in the /tests dir) and benchmarks (in the /benches dir). There are a couple of utility functions that I need in tests and benches, but they aren't ...
37
votes
2
answers
7k
views
Why can a Cargo package only have one library target?
According to its manual, Cargo packages can have multiple executable targets, but only one library target is allowed.
A package can contain zero or one library crates and as many binary crates as ...
36
votes
10
answers
30k
views
How to work with OpenSSL for Rust within a Windows development environment
I can't build my Rust project when I add an SSL dependency in my cargo file.
This isn't new to Windows, but I'd like to resolve the issue such that I can use Powershell and native Windows development ...
36
votes
3
answers
7k
views
How do I prevent `rust doc` from adding dependencies to documentation?
I've just started playing with Rust and was trying to generate docs for the code I've written. When I issued cargo doc, I saw something a little weird.
21:53 $ cargo doc
Compiling regex-syntax v0....