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
toml. Cargo. toml is the manifest file for Rust's package manager, cargo . This file contains metadata such as name, version, and dependencies for packages, which are call "crates" in Rust.
The cargo install command allows you to install and use binary crates locally. This isn't intended to replace system packages; it's meant to be a convenient way for Rust developers to install tools that others have shared on crates.io. You can only install packages that have binary targets.
Install Options. Specify a version to install. This may be a version requirement, like ~1.2 , to have Cargo select the newest version from the given requirement. If the version does not have a requirement operator (such as ^ or ~ ), then it must be in the form MAJOR.
Cargo is the Rust package manager. Cargo downloads your Rust package's dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community's package registry.
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:
Of course you can; you just have to do everything that Cargo does for you by hand. That involves
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With