我可以在没有 Cargo.toml 的情况下使用 Cargo 安装库吗? [英] Can I install a library using Cargo without a Cargo.toml?

查看:34
本文介绍了我可以在没有 Cargo.toml 的情况下使用 Cargo 安装库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Rust 的入门,我需要在我的系统上安装 rand crate.我不做货物包装的东西(例如创建 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.

我可以在我的系统上安装 rand 库而不使用 cargo 命令创建 Cargo.toml 吗?

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

推荐答案

实用答案

没有.使用货物.它使用起来极其简单,而且它可以防止您因管理版本(和冲突版本)而受到影响.

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.

从 99.9% 的 Rust 用户的角度来看,Cargo 是语言的一部分,或者至少是 Rust 生态系统的一部分.您可能会在其他语言的标准库中使用 crate 中提供的许多东西(随机数生成就是一个很好的例子).

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).

在我的系统上安装库

最终,这没有意义.没有可以安装的库的单一真实版本.每个使用 crate 的程序可能使用不同的版本,因为它有不同的需求.更进一步,您可以为不同的项目以不同的方式编译 crate - crate 具有特性,可以改变它们的编译方式.

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

这实际上是一种使用 Cargo 构建整个 Rust 项目的方法,该项目提供一个二进制并将其安装在您的系统上.这更有意义,因为它是一个单一的、包含的实体.不幸的是,正因为如此,它可能会令人困惑!

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!

另见:

当然你可以;您只需要手动完成 Cargo 为您所做的一切.这涉及

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

  1. 正在下载软件包.
  2. 这也意味着包的所有依赖项.
  3. 以及正确的版本.
  4. 编译包.
  5. 以及依赖项.
  6. 维护依赖关系树并将其传递给每个后续包.
  7. 最后,您可以编译代码了.

使用该库编译单个库和单个可执行文件的具体示例:

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

这篇关于我可以在没有 Cargo.toml 的情况下使用 Cargo 安装库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆