New to Rust: Virtual Environments?

Hi all,
I'm very new to Rust, but I'm hoping to use it for most of my hobby projects going forward. My experience is primarily with Python, where it's typical to create a new virtual environment for each project in order to keep packages isolated and and ensure that one project's dependencies don't mess with another project's dependencies.

How do Rust and Cargo handle dependency isolation? Is the concept of a virtual environment completely unnecessary in Rust? If so, what is the convention?

Alternatively, based on this link:
https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure

it looks like I could at least create a per-project .config file defining $CARGO_HOME to be where I want, which seems like it might replicate some of the Python virtual environment behavior. Is this a common, or at least sensible, approach?

Dependencies aren’t normally installed into the cargo directory in Rust. There is a cache directory that is shared, but it’s a true cache and therefore not a problem in the way a shared lib directory is.

Since dependencies aren’t shared there’s no real need for isolated per-project rust installs. Cargo will pull the correct dependencies for your project when you build it and link only those dependencies into the final program, regardless of what other programs you may have built before.

5 Likes

In general Rust's tooling has sensible defaults, if you don't have special requirements like wanting to work mostly offline or something like that, you don't need special setup. For example, when I set up Rust on a new PC, the most I'll do is set up mold as the default linker, and when I set up a new Rust project, the most I'll do is create a rustfmt.toml with some options that make the formatter more strict. Even these steps are completely optional and down to preference.

1 Like

Thanks for the answers!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.