21

I am going through Rust's getting started and I need to get the rand crate on my system. I'm not doing the Cargo packaging stuff (e.g. creating Cargo.toml) because I was interested in the language, not packaging.

Can I install the rand library on my system without creating a Cargo.toml using the cargo command?

$ cargo install rand
    Updating registry `https://github.com/rust-lang/crates.io-index`
specified package has no binaries
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Tomas Tomecek
  • 6,226
  • 3
  • 30
  • 26

1 Answers1

28

Practical answer

No. Use Cargo. It's extremely easy to use and it prevents you from shooting yourself in the foot with managing versions (and conflicting versions).

because I was interested in the language, not packaging.

From the point-of-view of 99.9% of Rust users, Cargo is part of the language, or at least part of the Rust ecosystem. Many things are provided in crates that you might expect in another languages standard library (random number generation is a great example).

install the library on my system

Ultimately, this doesn't make sense. There's no One True Version of a library that you can install. Every program that uses a crate may use a different version because it has different needs. Even further, you may compile a crate in a different manner for different projects - crates have features that change how they may be compiled.

cargo install rand

This is actually a way to use Cargo to build an entire Rust project that provides a binary and install it on your system. This makes more sense as it's a single, contained entity. Unfortunately, it can be confusing for just this very reason!

See also:

Technically correct answer

Of course you can; you just have to do everything that Cargo does for you by hand. That involves

  1. Downloading the package.
  2. This also means any dependencies of the package.
  3. And the correct versions.
  4. Compile the package.
  5. And the dependencies.
  6. Maintaining the tree of dependencies and passing it to each subsequent package.
  7. Finally, you can compile your code.

A concrete example of compiling a single library and a single executable using that library:

$ rustc --edition=2018 --crate-type=rlib --crate-name library_example src/lib.rs -o libmy_library.rlib
$ rustc --edition=2018 --extern library_example=libmy_library.rlib examples/main.rs
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thanks for such detailed answer. Small followup question: so I'm in my project; when I run `cargo build`, cargo will compile all dependencies and store them in `target/debug/deps/`? – Tomas Tomecek Apr 23 '16 at 17:23
  • 3
    @SummerBreeze yep! And when you compile in release mode it's `target/release` and docs are in `target/docs`. Downloaded *source code* is placed in `$HOME/.cargo` because it can be shared between projects. The built artifacts are project-specific. – Shepmaster Apr 23 '16 at 17:25
  • 4
    I wish more people were this helpful; thank you very much! – Tomas Tomecek Apr 23 '16 at 17:36
  • Here's a use case for "installing" a library I just run into: I'm trying to cargo install some-thing which requires 172 dependencies. Dependency #115 fails to build due to some system configuration issue. Fiddling with the system configuration I need to re-run the "cargo install some-thing" command, wait for the first 114 dependencies to build (yes, in parallel, still takes a while and sucks) to get to the failure. If I could do `cargo can-install the-specific-dependency` which would behave AS IF a crate required the dependency and just try to build it, it would ease the pain a lot. – Oren Ben-Kiki Mar 03 '21 at 08:54
  • @OrenBen-Kiki in that case, `cargo install` will print out the directory it used (probably in a system temporary directory). You can then use `cargo install --target-dir ...` with that path to resume from the failed path. – Shepmaster Mar 08 '21 at 16:00
  • @OrenBen-Kiki moved that to [How can I avoid rebuilding dependencies when `cargo install` fails due to a system configuration issue?](https://stackoverflow.com/q/66533502/155423) – Shepmaster Mar 08 '21 at 16:28