Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I install a library using Cargo without a Cargo.toml?

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
like image 654
Tomas Tomecek Avatar asked Apr 23 '16 17:04

Tomas Tomecek


People also ask

What is cargo toml?

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.

How does cargo install work?

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.

How do I install a specific version of cargo?

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.

How does cargo work Rust?

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.


1 Answers

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:

  • Error installing a crate via cargo: specified package has no binaries
  • cargo install <library_name> --library (Cargo issue #2552)

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
like image 96
Shepmaster Avatar answered Oct 14 '22 19:10

Shepmaster