Rust 1.58.0(Rust 1.58.0)

  Rust 1.58.0 现已发布,该版本带来了在格式字符串中捕获的标识符、改变了 Windows 上的 Command 搜索路径,在标准库中增加了 #[must_use] 注释,以及一些新的库稳定性。如果你此前已通过 rustup 安装了以前的 Rust 版本,运行以下命令即可升级至最新版本:

  rustup update stable

  具体更新内容包括:

  格式字符串中捕获的标识符

  格式字符串现在可以通过在字符串中写入 {ident} 来捕获参数。格式长期以来接受位置参数(可选地通过索引)和命名参数,例如:

  println!(“Hello, {}!”, get_person()); // implicit position

  println!(“Hello, {0}!”, get_person()); // explicit index

  println!(“Hello, {person}!”, person = get_person()); // named

  现在命名参数也可以从周围的范围中捕获,例如:

  let person = get_person();

  // …

  println!(“Hello, {person}!”); // captures the local `person`

  这也可以用于格式化参数:

  let (width, precision) = get_format();

  for (name, score) in get_scores() {

  println!(“{name}: {score:width$.precision$}”);

  }

  格式字符串只能捕获普通标识符,不能捕获任意路径或表达式。对于更复杂的参数,要么先将它们分配给本地名称,要么使用旧name = expression样式的格式化参数。

  减少 WindowsCommand搜索路径

  在 Windows targets 上,std::process::Command 将不再搜索当前目录中的可执行文件。Rust 现在在没有当前目录的情况下执行自己的搜索,并且不包括旧的 16 位目录,因为没有 API 来发现它的位置。所以 Windows 上 Rust 的新Command搜索顺序是:

  PATH子环境变量中列出的目录。

  加载应用程序的目录。

  32 位 Windows 系统目录。

  Windows 目录。

  PATH环境变量中列出的目录。

  Non-Windows targets 继续使用其特定于平台的行为,通常只考虑子或父PATH环境变量。

  标准库中更多的#[must_use]

  Library proposal 35 在 2021 年 10 月被批准,以审核和扩大 #[must_use] 在整个标准库中的应用,涵盖更多以返回值为主要作用的 functions。这类似于 function purity 的想法,但比真正的语言特性要宽松。其中一些新增功能在 1.57.0 版本中出现过,现在在 1.58.0 版本中已完成。

  稳定的 API

  以下方法和特性的实现被稳定化:

  Metadata::is_symlink

  Path::is_symlink

  {integer}::saturating_div

  Option::unwrap_unchecked

  Result::unwrap_unchecked

  Result::unwrap_err_unchecked

  File::options

  以下以前稳定的功能现在是const。

  Duration::new

  Duration::checked_add

  Duration::saturating_add

  Duration::checked_sub

  Duration::saturating_sub

  Duration::checked_mul

  Duration::saturating_mul

  Duration::checked_div

  更多详情可查看官方博客。

————————

Rule 1.58.0 has been released. This version brings the identifier captured in the format string, changes the command search path on windows, adds #[must_use] comments in the standard library, and some new library stability. If you have previously installed the previous version of rust through rustup, run the following command to upgrade to the latest version:

  rustup update stable

Specific updates include:

Identifier captured in format string

Format strings can now capture parameters by writing {ident} to the string. Formats have long accepted positional parameters (optionally by index) and named parameters, such as:

  println!(“Hello, {}!”, get_person()); // implicit position

  println!(“Hello, {0}!”, get_person()); // explicit index

  println!(“Hello, {person}!”, person = get_person()); // named

Now named parameters can also be captured from the surrounding range, for example:

  let person = get_person();

// …

  println!(“Hello, {person}!”); // captures the local `person`

This can also be used to format parameters:

  let (width, precision) = get_format();

  for (name, score) in get_scores() {

  println!(“{name}: {score:width$.precision$}”);

}

The format string can only capture ordinary identifiers and cannot capture any path or expression. For more complex parameters, either assign them to local names first, or use the formatting parameters of the old name = expression style.

Reduce windows command search paths

On Windows targets, STD:: process:: command will no longer search for executable files in the current directory. Rust now performs its own search without the current directory and does not include the old 16 bit directory because there is no API to discover its location. So the new search order of rust command on windows is:

The directory listed in the path sub environment variable.

The directory where the application is loaded.

32-bit windows system directory.

Windows directory.

The directory listed in the path environment variable.

Non windows targets continues to use its platform specific behavior, usually considering only child or parent path environment variables.

More #[must_use in the standard library

Library proposal 35 was approved in October 2021 to review and expand #[must_use] in the whole standard library, covering more functions with return value as the main function. This is similar to the idea of function purity, but looser than the real language features. Some of these new features appeared in version 1.57.0 and have been completed in version 1.58.0.

Stable API

The implementation of the following methods and features is stabilized:

  Metadata::is_symlink

  Path::is_symlink

  {integer}::saturating_div

  Option::unwrap_unchecked

  Result::unwrap_unchecked

  Result::unwrap_err_unchecked

  File::options

The following previously stable functions are now const.

  Duration::new

  Duration::checked_add

  Duration::saturating_add

  Duration::checked_sub

  Duration::saturating_sub

  Duration::checked_mul

  Duration::saturating_mul

  Duration::checked_div

See the official blog for more details.