Cargo: How to use a different directory to store source files


#1

Can cargo already use an other directory, in place of the ./src/?

Documentation indicates CARGO_TARGET_DIR for the target directory, but doesn’t seem to indicate anything about the source directory.

Also, being able to change that directly in the .TOML configuration of the project would be even better.

If that is not yet possible, any guidance on how and where propose a patch to make that possible is very welcome too.

Thank in advance for any answer and any feedback you would like to provide. Moreover, make everybody a favor in avoiding to wast resource in explainations on how follow rigidly fixed conventions is better to empower users with flexibility they can use at their own discretion regardless of the fine defaults.

Cheers


#2

Source is configured on per-target basis, via the path attribute.

Docs: https://doc.rust-lang.org/cargo/reference/manifest.html#configuring-a-target


#3

Great, thank you @matklad, so far I’ve been able to change the source and target directory, combining the export CARGO_TARGET=./celo setting with Cargo.toml following configuration:

[package]
name = "salutilo"
version = "0.1.0"

[[bin]]
name="saluti"
path = "fontaro/halo.rs"

I see that then celo includes a full directory hierarchy that I would find interesting to customize too (debug/build and so on…). It’s less essential but wonder anyway if there are already facilities to change that.

Thank you again.


#4

Target director is mostly(but not entirely) a private scratch space for Cargo, so it is not customizable.

What could be tweaked in theory is the place where the final product of the build is stored. It is not customizable just yet, though ther’s an unstable —output-dir flag for that use case.

BTW, I think you might find .cargo/config file more convenient for overriding the target dir than the env var. Docs: https://doc.rust-lang.org/cargo/reference/config.html


#5

Ho great, thank you very much @matklad it sounds indeed far more handy. :slight_smile: