How to set feature options for cargo build? - rust

I use this kind of things:
#[cfg(feature = "myfeature")]
When I click "Build - Build Project" it calls cargo build. How can I specify that I want myfeature to be used? I found that I can specify it for my Run configuration but still it doesn't work for "Build".

On the command line, you'd use cargo build --features myfeature. In clion using the IntelliJ Rust plugin, you should be able to open the Cargo.toml file and tick the box next to the feature definition to enable it within the IDE, as demonstrated in this blog post.

Related

Override the [[bin]] path parameter in `cargo run`

I have a local playground setup with a bunch of experiment files (each with a main()), where my Cargo.toml looks like this:
[package]
name = "playground"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "playground"
path = "./experiment42.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
Right now, I'm manually changing the path parameter when I want to debug one or the other experiment and I want to override it in cargo run command.
I tried variants of --config <KEY=VALUE> based from the docs to no avail:
$ cargo run --config "bin.path = \"./experiment69.rs\""
# still runs experiment42.rs
$ cargo run --config "bin.'cfg(all(name = \"playground\"))'.path = \"./experiment69.rs\""
# still runs experiment42.rs
The overall goal is to run different files based on the currently opened file in my editor (which I can send to cargo run args). Adding new experiment files without editing Cargo.toml would be a nice bonus.
Since you want cargo to pick up your binary files automatically you could use cargos automatic binary detection by adding them to the directory cargo picks binaries up automatically, src/bin/ then you can remove all [[bin]] sections and run it with cargo run --bin yourbinaryfilename for the file src/bin/yourbinaryfilename.rs
For how to do this automatically for the file you've opened in your editor we'd have to know what editor you're using, but for vim you can call cargo with :!cargo run --bin %:t:r where %:t:r is the root of the current filename.
Cargo configurations (.cargo/config.toml) are different from the manifest files (Cargo.toml). --config <KEY=VALUE> is meant to override the former. A mechanism for overriding manifest values is proposed, but not yet implemented.
I ended up running a sed command before cargo for now, similar to the person in the linked issue:
sed -i -e 's/^path= .*/path = "%file_path%"/' Cargo.toml

Set a project to use nightly by default

How do I set a Cargo project to build & run using nightly by default (i.e. cargo build is actually cargo +nightly build) without setting nightly as the global default?
This is not the same question as How to switch between Rust toolchains. If you read both questions you'll see that question is asking about switching Rust toolchains globally whereas I want to switch Rust toolchains without changing the global setting.
With rustup override set nightly it sets the default for that directory to nightly:
https://rust-lang.github.io/rustup/overrides.html#directory-overrides
If you want this choice to also be reflected in the repository, as opposed to just local configuration, you can add a rust-toolchain.toml file, for example
[toolchain]
channel = "nightly"
You might also want to set the nightly compiler's version (printed by rustup show) in the rust-version field of your cargo.toml, so that dependents which try to build on stable get an error message (since rust-toolchain.toml sets the version for a directory, not a crate).

How can I run a command::new(...) during cargo clean?

While I am learning rust, I have a crude means of building a non-rust c/c++ library that lives in a submodule. My build script (build.rs) now looks like:
use std::process::Command;
fn main() {
// EXTERN_C
// Build the c-library
Command::new("make").args(&["-C", "cadd"]).status().unwrap();
// The name of the library to link to, i.e. like: -l<lib>
println!("cargo:rustc-link-lib=dylib=add_x64Linuxd");
// The library search path for linking, i.e. like -L<path>
println!("cargo:rustc-link-search=native=cadd/lib");
// The run-time library search path (LD_LIBRARY_PATH)
println!("cargo:rustc-env=LD_LIBRARY_PATH=cadd/lib");
}
This is working nicely, the makefile within cadd/ sorts out any build/re-build deps etc. The only thing I can't do at the moment is hook in make -C cadd clean when I run cargo clean. Ideally I would like it to run the clean make target at the same time. The command would look like:
Command::new("make").args(&["-C", "cadd", "clean"]).status().unwrap();
But I don't know how to get such a command to run during cargo clean. Is there a "clean script" like there is a "build script" - or another method?
Eventually I will get around to learning how to wrap up my makefile project into a cargo crate (I think that's the right terminology) - so I know this is not the optimal way to do this, but I want to get this working in a basic way first (so my head does not explode!).
The cargo clean command simply deletes the cargo target directory.
One solution is for your Makefile to output its compilation artifacts(all files it generates) into the target directory.
You could also change the directory cargo outputs its artifacts to, either via the --target-dir CLI option or by adding the following to the .cargo/config:
[build]
target-dir = "some/path"

Rust pkg mgr Cargo -how can I set a default VCS so each "cargo new projname" is set for that VCS

where in cargo's set of base configurations can I tell cargo to create new projects with a VCS that isn't github?
want to do this 1 time, not on every "cargo new" and not via editing ea project config
The cargo config is described in the cargo book chapter 3.3.
By default cargo new will initialize a new Git repository. This key can be set to hg to create a Mercurial repository, or none to disable this behavior.
vcs = "none"
Adding details to hellow's answer, the file is ~/.cargo/config and you should create it if it doesn't exist already. Then add the following:
[cargo-new]
vcs = "none" # VCS to use ('git', 'hg', 'pijul', 'fossil', 'none')

Is it possible to deactivate file locking in cargo?

I want to run the following commands side by side
cargo watch "check"
cargo watch "build"
I want to run cargo watch build in the background and use cargo watch check to look at the error messages.
The problem is that cargo watch check always runs after cargo watch build and then also needs to wait on the file lock
cargo check
Blocking waiting for file lock on build directory
I don't think that a file lock would be required for cargo check. Is it possible to disable file locking in cargo?
I don't think that a file lock would be required for cargo check.
I can think of in one reason: build scripts. A build script can generate files that are included in the crate, checking the crate without generating the files would probably produce errors. Running 2 instances of a build script in parallel is not a good idea (conflicting file writes, etc), so the locking is required.
I want to run the following commands side by side
You have two options:
Sequential: install cargo-do and run
cargo watch "do check, build"
this will first run cargo check and then cargo build (if cargo check did not find an error).
Parallel: change the target-dir for one of the two cargo commands:
CARGO_TARGET_DIR=/tmp cargo watch check

Resources