You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Rust 新版解读 | 1.76 | ABI 兼容性更新
> Rust 1.76 官方 release doc: [Announcing Rust 1.76.0 | Rust Blog](https://blog.rust-lang.org/2024/02/08/Rust-1.76.0.html)
通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.76 版本:
```shell
$ rustup update stable
```
2024 新年好!
## ABI 兼容性更新
函数指针文档中新的 [ABI 兼容性部分](https://doc.rust-lang.org/stable/std/primitive.fn.html#abi-compatibility)描述了函数签名与 ABI 兼容的含义,并包括一份当前 Rust 中参数和返回值类型符合 ABI 兼容的列表。在大多数情况下,本文档不会添加任何新的保证,仅描述现有的兼容性状态。
当前保证了 `char``u32` 是 ABI 兼容的。它们始终具有相同的大小和对齐方式,也被认为是完全等效的。
## 通过引用获取类型名称描述
出于调试目的,自 Rust 1.38 起,`any::type_name::<T>()` 可用于获取类型 `T` 的字符串描述,但这需要显式写明类型。而写清楚类型并不总是那么容易,特别是对于像闭包这样的不可命名类型或不透明的返回类型。新的 `any::type_name_of_val(&T)` 允许使用任何对象的引用,来获取对应类型的描述性名称。
```rust
fn get_iter() -> impl Iterator<Item = i32> {
[1, 2, 3].into_iter()
}
fn main() {
let iter = get_iter();
let iter_name = std::any::type_name_of_val(&iter);
let sum: i32 = iter.sum();
println!("The sum of the `{iter_name}` is {sum}.");
}
```
上述代码将会打印出
```text
The sum of the `core::array::iter::IntoIter<i32, 3>` is 6.
```
## Others
其它更新细节和稳定的API列表参考[原Blog](https://blog.rust-lang.org/2024/02/08/Rust-1.76.0.html#stabilized-apis)