Rust 1.29版本

Rust开发团队很高兴地宣布发布Rust的新版本:1.29.0。 Rust是一种针对安全性,速度和并行代码执行的系统编程语言。


如果您使用Rustup安装了Rust的早期版本,那么只需将Rust升级到1.29.0版本,您只需执行以下操作:


$ rustup update stable 

如果尚未安装Rustup,则可以从我们网站的相应页面进行安装详细的Rust 1.29.0发行说明可在GitHub上获得。


稳定版1.29.0中包含什么


1.29所做的更改不多。 Rust 1.30和1.31预计将非常重要,因此1.29迭代的大部分都已为将来的更改做准备。 此版本的两个最显着的创新甚至不涉及语言本身:这是Cargo的两个新功能,并且都与警告有关。


  • cargo fix自动修复代码中的警告
  • cargo clippy -静态Rust代码分析器,可帮助捕获常见错误并仅改进代码

cargo fix


随着Rust 1.29的发布,Cargo有了一个新的子命令: cargo fix 。 如果您曾经用Rust编写过,很可能会遇到编译器警告。 例如,考虑以下代码:


 fn do_something() {} fn main() { for i in 0..100 { do_something(); } } 

在其中,我们调用do_something一百次,但是从不使用i变量。 Rust对此警告我们:


 > cargo build Compiling myprogram v0.1.0 (file:///path/to/myprogram) warning: unused variable: `i` --> src\main.rs:4:9 | 4 | for i in 1..100 { | ^ help: consider using `_i` instead | = note: #[warn(unused_variables)] on by default Finished dev [unoptimized + debuginfo] target(s) in 0.50s 

看到有关重命名为_i的提示? 我们可以将其与cargo fix一起自动应用:


 > cargo fix Checking myprogram v0.1.0 (file:///C:/Users/steve/tmp/fix) Fixing src\main.rs (1 fix) Finished dev [unoptimized + debuginfo] target(s) in 0.59s 

如果现在打开src\main.rs ,我们将看到更正的代码:


 fn do_something() {} fn main() { for _i in 0..100 { do_something(); } } 

现在,在代码中使用了_i ,并且不再发出警告。


cargo fix程序的第一个版本不能修复所有警告。 cargo fix使用特殊的编译器API进行工作,该API仅修复我们绝对确定的那些警告。 随着时间的流逝,他们的名单将会扩大。


cargo clippy


有关警告的更多信息:现在,您可以尝试通过Rustup进行cargo-clippy固定。 Clippy是一个静态分析器,它对您的代码执行许多其他检查。


例如:


 let mut lock_guard = mutex.lock(); std::mem::drop(&lock_guard) operation_that_requires_mutex_to_be_unlocked(); 

从语法上讲,这是正确的代码,但是我们可以得到一个死锁,因为我们为lock_guard _链接到_而不是lock_guard本身调用了drop 。 调用链接的drop毫无意义,几乎可以肯定是一个错误。


通过Rustup安装Clippy的初始版本:


 $ rustup component add clippy-preview 

并运行它:


 $ cargo clippy error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing. --> src\main.rs:5:5 | 5 | std::mem::drop(&lock_guard); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: #[deny(drop_ref)] on by default note: argument has type &std::result::Result<std::sync::MutexGuard<'_, i32>, std::sync::PoisonError<std::sync::MutexGuard<'_, i32>>> --> src\main.rs:5:20 | 5 | std::mem::drop(&lock_guard); | ^^^^^^^^^^^ = help: for further information visit https://rust-lang-nursery.imtqy.com/rust-clippy/v0.0.212/index.html#drop_ref 

从消息的注释中可以看到,您可以通过链接获得所有可能的警告的完整列表。


请注意,这只是一个试用版; Clippy尚未达到1.0,因此检查的设置和行为仍然可以更改。 稳定后,我们会立即发布该clippy组件,但现在,请您查看初步版本并告诉我们您的经验。


是的,仍然有一个细微差别:不幸的是,您不能将clippy与cargo-fix一起使用。 这项工作正在进行中。


有关更多详细信息,请参见发行说明


稳定标准库


此版本中稳定了以下API:



另外,现在您可以 &strOsString


有关更多详细信息,请参见发行说明


货运增强


上面,我们已经描述了两个新的Cargo子命令。 此外, Cargo Cargo.lock , git merge 。 可以使用--locked标志禁用此行为。


cargo doc有了一个新标记:-- --document-private-items 。 默认情况下, cargo doc仅记录API的公共部分,因为它旨在生成用户文档。 但是,如果您正在处理软件包并且它具有内部文档,则--document-private-items将启用所有内容的文档生成。


有关更多详细信息,请参见发行说明


开发人员1.29.0


很多人参与了Rust 1.29的开发。 没有你们每个人,我们不可能完成工作。 谢谢你


来自翻译者:感谢@Revertron帮助翻译。

Source: https://habr.com/ru/post/zh-CN423249/


All Articles