Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help


x-i18n: generated_at: “2026-03-01T14:57:45Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: dc250ffa5f82d92ee01e7a6bf46ac6fb7b1f71f0ae368bbb5fe88d71b1f6e5e5 source_path: ch20-00-advanced-features.md workflow: 16

高级特性 (Advanced Features)

Advanced Features

到目前为止,你已经学习了 Rust 编程语言中最常用的部分。在我们在第 21 章再做一个项目之前,我们将看看该语言中你可能偶尔会遇到但可能不会每天都使用的几个方面。当遇到任何未知内容时,你可以将本章作为参考。这里介绍的特性在非常特定的情况下很有用。虽然你可能不会经常用到它们,但我们希望确保你掌握 Rust 所提供的所有特性。

By now, you’ve learned the most commonly used parts of the Rust programming language. Before we do one more project, in Chapter 21, we’ll look at a few aspects of the language you might run into every once in a while but may not use every day. You can use this chapter as a reference for when you encounter any unknowns. The features covered here are useful in very specific situations. Although you might not reach for them often, we want to make sure you have a grasp of all the features Rust has to offer.

在本章中,我们将涵盖:

  • 不安全 Rust (Unsafe Rust):如何选择退出 Rust 的某些保证,并承担手动维护这些保证的责任
  • 高级特征 (Advanced traits):关联类型、默认类型参数、完全限定语法、父特征 (supertraits) 以及与特征相关的 newtype 模式
  • 高级类型 (Advanced types):关于 newtype 模式的更多信息、类型别名、从不类型 (never type) 和动态大小类型
  • 高级函数和闭包 (Advanced functions and closures):函数指针和返回闭包
  • 宏 (Macros):在编译时定义定义更多代码的代码的方法

In this chapter, we’ll cover:

  • Unsafe Rust: How to opt out of some of Rust’s guarantees and take responsibility for manually upholding those guarantees
  • Advanced traits: Associated types, default type parameters, fully qualified syntax, supertraits, and the newtype pattern in relation to traits
  • Advanced types: More about the newtype pattern, type aliases, the never type, and dynamically sized types
  • Advanced functions and closures: Function pointers and returning closures
  • Macros: Ways to define code that defines more code at compile time

这是 Rust 特性的汇集,每个人都能各取所需!让我们潜入其中吧!

It’s a panoply of Rust features with something for everyone! Let’s dive in!