可以在没有标准库的情况下编译 Rust 代码吗? [英] Can Rust code compile without the standard library?

查看:63
本文介绍了可以在没有标准库的情况下编译 Rust 代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Rust.我主要使用 The Rust Programming Language 书和 这个很好的参考 它将 Rust 特性/语法与 C++ 等价物联系起来.

I'm currently in the progress of learning Rust. I'm mainly using The Rust Programming Language book and this nice reference which relates Rust features/syntax to C++ equivalents.

我很难理解核心语言在哪里停止而标准库从哪里开始.我遇到了很多似乎与编译器有特殊关系的运算符和/或特征.例如,Rust 有一个名为 Dereftrait(据我所知就像一个接口),它让我们使用 * 取消引用实现它的类型 运算符:

I'm having a hard time understanding where the core language stops and the standard library starts. I've encountered a lot of operators and/or traits which seems to have a special relationship with the compiler. For example, Rust has a trait (which from what I understand is like an interface) called Deref which let's a type implementing it be de-referenced using the * operator:

fn main() {
    let x = 5;
    let y = Box::new(x);

    assert_eq!(5, x);
    assert_eq!(5, *y);
}

另一个例子是 ? 运算符,它似乎取决于 ResultOption 类型.

Another example is the ? operator, which seems to depend on the Result and Option types.

使用这些运算符的代码可以在没有标准库的情况下编译吗?如果不是,那么 Rust 语言的哪些部分取决于标准库?甚至可以在没有它的情况下编译任何 Rust 代码吗?

Can code that uses those operators can be compiled without the standard library? And if not, what parts of the Rust language are depending on the standard library? Is it even possible to compile any Rust code without it?

推荐答案

Rust 标准库实际上分为三个不同的 crate:

The Rust standard library is in fact separated into three distinct crates:

  • core,这是语言和标准库之间的粘合剂.语言所需的所有类型、特征和函数都可以在这个 crate 中找到.这包括操作符特征(在 core::ops),未来 trait(由 async fn 使用)和 编译器内在函数.core crate 没有任何依赖项,因此您可以随时使用它.
  • alloc,其中包含类型以及与动态内存分配相关或需要动态内存分配的特征.这包括动态分配的类型,例如 BoxVecString.
  • std,其中包含整个标准库,包括来自 corealloc 的东西,还有一些有进一步要求的东西,比如文件系统访问、网络等.
  • core, which is the glue between the language and the standard library. All types, traits and functions required by the language are found in this crate. This includes operator traits (found in core::ops), the Future trait (used by async fn), and compiler intrinsics. The core crate does not have any dependencies, so you can always use it.
  • alloc, which contains types and traits related to or requiring dynamic memory allocation. This includes dynamically allocated types such as Box<T>, Vec<T> and String.
  • std, which contains the whole standard library, including things from core and alloc but also things with further requirements, such as file system access, networking, etc.

如果您的环境不提供 std crate 所需的功能,您可以选择在没有它的情况下进行编译.如果您的环境也不提供动态内存分配,您也可以选择不使用 alloc crate 进行编译.此选项对于嵌入式系统或编写操作系统等目标很有用,在这些目标中,您通常不会拥有标准库通常需要的所有东西.

If your environment does not provide the functionality required by the std crate, you can choose to compile without it. If your environment also does not provide dynamic memory allocation, you can choose to compile without the alloc crate as well. This option is useful for targets such as embedded systems or writing operating systems, where you usually won't have all of the things that the standard library usually requires.

您可以使用 #![no_std] 属性位于 crate 的根目录中,告诉编译器在没有标准库的情况下进行编译(仅 core).许多库通常也支持no-std"编译(例如 base64>futures),其中功能可能受到限制,但在没有 std crate 的情况下编译时会起作用.

You can use the #![no_std] attribute in the root of your crate to tell the compiler to compile without the standard library (only core). Many libraries also usually support "no-std" compilation (e.g. base64 and futures), where functionality may be restricted but it will work when compiling without the std crate.

这篇关于可以在没有标准库的情况下编译 Rust 代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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