Rust 1.74.0 发布

2023年 11月 18日 68.3k 0

Rust 1.74.0 稳定版已正式发布,主要带来以下变化:

通过 Cargo 进行 Lint 配置

正如 RFC 3389 所提议,Cargo.tomlmanifest 现在支持一个[lints]表来配置来自编译器和其他工具的 lints 的报告级别(禁止、拒绝、警告、允许)。因此,不要使用-F/-D/-W/-A设置 RUSTFLAGS(这会影响整个构建过程),或者使用 crate-level 的属性,例如:

#![forbid(unsafe_code)]
#![deny(clippy::enum_glob_use)]

现在可以将这些写入 package manifest 中以供 Cargo 处理:

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
enum_glob_use = "deny"

这些也可以在[workspace.lints]表中配置,然后像许多其他工作区设置一样由[lints] workspace = true继承。在决定哪些 crates 需要重建时,Cargo 还将跟踪这些设置的更改。

有关详细信息,可参阅 Cargo 参考手册中的lintsworkspace.lints部分。

Cargo Registry Authentication

此版本中还包含两个相关的 Cargo 功能:credential providers 和 authenticated private registries。

Credential providers 允许配置 Cargo 如何获取注册表的凭证。Built-in providers 用于 Linux、macOS 和 Windows 上特定于操作系统的安全秘密存储。此外,可以编写自定义 providers 来支持存储或生成令牌的任意方法。使用安全的 credential provider 可以降低注册表令牌泄漏的风险。

Registries 现在可以选择要求对所有操作进行身份验证,而不仅仅是发布。这使得 private Cargo registries 能够提供更安全的 crates 托管。使用 private registries 需要配置 credential provider。

有关更多信息,可参阅 Cargo 文档。

Projections in opaque return types

有关返回“return type cannot contain a projection or Self that references lifetimes from a parent scope”错误信息的问题现在已经解决。编译器现在允许在 opaque return types 中提及Self和关联类型,例如async fn-> impl Trait。即使你对"projection"之类的术语一无所知,这种功能也能让 Rust 更接近你所期望的工作方式。

不过该功能目前有一个不稳定的 feature gate,因为它的实现最初没有正确处理 captured lifetimes。有关更多技术细节,可参阅 stabilization pull request。示例:

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> { /* ... */ }
}

Stabilized APIs

  • core::num::Saturating
  • impl From<io::Stdout> for std::process::Stdio
  • impl From<io::Stderr> for std::process::Stdio
  • impl From<OwnedHandle> for std::process::Child{Stdin, Stdout, Stderr}
  • impl From<OwnedFd> for std::process::Child{Stdin, Stdout, Stderr}
  • std::ffi::OsString::from_encoded_bytes_unchecked
  • std::ffi::OsString::into_encoded_bytes
  • std::ffi::OsStr::from_encoded_bytes_unchecked
  • std::ffi::OsStr::as_encoded_bytes
  • std::io::Error::other
  • impl TryFrom<char> for u16
  • impl<T: Clone, const N: usize> From<&[T; N]> for Vec<T>
  • impl<T: Clone, const N: usize> From<&mut [T; N]> for Vec<T>
  • impl<T, const N: usize> From<[T; N]> for Arc<[T]>
  • impl<T, const N: usize> From<[T; N]> for Rc<[T]>

这些 API 现在在 const contexts 中是稳定的:

  • core::mem::transmute_copy
  • str::is_ascii
  • [u8]::is_ascii

兼容性说明

  • 正如之前所宣布的,Rust 1.74 提高了对 Apple 平台的要求。现在最低版本是:
    • macOS:10.12 Sierra(2016 年首次发布)
    • iOS:10(2016 年首次发布)、
    • tvOS:10(2016 年首次发布)

其他变化

查看 Rust、Cargo 和 Clippy 中发生的所有变化。

详情可查看官方公告。

相关文章

塑造我成为 CTO 之路的“秘诀”
“人工智能教母”的公司估值达 10 亿美金
教授吐槽:985 高校成高级蓝翔!研究生基本废了,只为房子、票子……
Windows 蓝屏中断提醒开发者:Rust 比 C/C++ 更好
Claude 3.5 Sonnet 在伽利略幻觉指数中名列前茅
上海新增 11 款已完成登记生成式 AI 服务

发布评论