|
|
@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
# Rust 新版解读 | 1.74 | 通过 Cargo 配置 Lint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
> Rust 1.74 官方 release doc: [Announcing Rust 1.74.0 | Rust Blog](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.74 版本:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
$ rustup update stable
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 通过 Cargo 配置 Lint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如 [RFC 3389](https://rust-lang.github.io/rfcs/3389-manifest-lint.html) 提案,如今 `Cargo.toml` 支持通过 `[lints]` 表格来配置来自编译器或其它检查工具的 lints 的警告等级(forbid,deny,warn,allow)。等效于之前 crate 级别的属性:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
|
|
|
#![deny(clippy::enum_glob_use)]
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如今可以通过配置 `Cargo.toml` 达到同样的效果:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```toml
|
|
|
|
|
|
|
|
[lints.rust]
|
|
|
|
|
|
|
|
unsafe_code = "forbid"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[lints.clippy]
|
|
|
|
|
|
|
|
enum_glob_use = "deny"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
同时这些配置也可以在 `[workspace.lints]` 里配置,然后通过 `[lints] workspace = true` 的形式在 workspace 里的每个项目里继承这些配置。Cargo 会动态跟踪这里的配置变化,以在需要的时候重新编译检查。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
更多信息,可以参考 Cargo 参考手册里的 [`lints`](https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-lints-section) 和 [`workspace.lints`](https://doc.rust-lang.org/stable/cargo/reference/workspaces.html#the-lints-table) 章节。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Cargo 注册服务认证
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
两个新的特性:自定义凭据提供商(credential providers)和 私有注册服务认证(authenticated private registries)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
除了内置的和系统相关的安全凭据认证外,现在支持通过指定自定义的凭据提供商来生成和保存tokens,减少了注册服务token泄露的可能性。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
注册服务如今对于所有操作都可以要求认证,(之前仅仅是发布操作),这对私有注册服务提供了更好的安全性。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
更多相关信息可以参考 [Cargo docs: registry-authentication](https://doc.rust-lang.org/beta/cargo/reference/registry-authentication.html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 模糊返回类型推断
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
或许你也曾被编译器警告过:"return type cannot contain a projection or `Self` that references lifetimes from a parent scope"。如今这点得到了改善,编译器如今允许在返回类型里使用 `Self` 和关联类型 ( `async fn` 和 `impl Trait` )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
这个更新让 Rust 编译器变得更符合你*期望*它应该正常工作的样子,即使你都不知道这个 "projection" 的术语的含义是什么 :)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
之前做不到这一点的原因是无法正常处理一些使用到的声明周期标注,更多技术细节可以看这个 [stabilization pull request](https://github.com/rust-lang/rust/pull/115659),其中提到了如今可以写出这样的代码:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
|
|
|
struct Wrapper<'a, T>(&'a T);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Opaque return types that mention `Self`:
|
|
|
|
|
|
|
|
impl Wrapper<'_, ()> {
|
|
|
|
|
|
|
|
async fn async_fn() -> Self { /* ... */ }
|
|
|
|
|
|
|
|
fn impl_trait() -> impl Iterator<Item = Self> { /* ... */ }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
trait Trait<'a> {
|
|
|
|
|
|
|
|
type Assoc;
|
|
|
|
|
|
|
|
fn new() -> Self::Assoc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Trait<'_> for () {
|
|
|
|
|
|
|
|
type Assoc = ();
|
|
|
|
|
|
|
|
fn new() {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Opaque return types that mention an associated type:
|
|
|
|
|
|
|
|
impl<'a, T: Trait<'a>> Wrapper<'a, T> {
|
|
|
|
|
|
|
|
async fn mk_assoc() -> T::Assoc { /* ... */ }
|
|
|
|
|
|
|
|
fn a_few_assocs() -> impl Iterator<Item = T::Assoc> { /* ... */ }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Others
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
其它更新细节,和稳定的API列表,参考[原Blog](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#stabilized-apis)
|