Merge pull request #22582 from mcfd/master

translate
This commit is contained in:
Xingyu.Wang 2021-07-17 11:00:39 +08:00 committed by GitHub
commit 02925850cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 55 deletions

View File

@ -1,55 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (mcfd)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (My top 7 keywords in Rust)
[#]: via: (https://opensource.com/article/20/10/keywords-rust)
[#]: author: (Mike Bursell https://opensource.com/users/mikecamel)
My top 7 keywords in Rust
======
Learn a handful of useful keywords from the Rust standard library.
![Rustacean t-shirt][1]
I've been using [Rust][2] for a few months now, writing rather more of it than I expected—though quite a lot of that has been thrown away as I've learned, improved what I'm writing, and taken some more complex tasks beyond what I originally intended.
I still love it and thought it might be good to talk about some of the important keywords that come up again and again in Rust. I'll provide my personal summary of what they do, why you need to think about how you use them, and anything else that's useful, particularly for people who are new to Rust or coming from another language (such as Java; see my article [_Why I'm enjoying learning Rust as a Java programmer_][3]).
Without further ado, let's get going. A good place for further information is always the official Rust documentation—you'll probably want to start with the [std library][4].
1. **const**  You get to declare constants with const, and you should. This isn't rocket science, but do declare with const, and if you're going to use constants across different modules, then do the right thing and create a `lib.rs` file (the Rust default) into which you can put all of them with a nicely named module. I've had clashes of const variable names (and values!) across different files in different modules simply because I was too lazy to do anything other than cut and paste across files when I could have saved myself lots of work simply by creating a shared module.
2. **let**  You don't _always_ need to declare a variable with a let statement, but your code will be clearer when you do. What's more, always add the type if you can. Rust will do its very best to guess what it should be, but it may not always be able to do so at runtime (in which case [Cargo][5], the compiler, will tell you), or it may even not do what you expect. In the latter case, it's always simpler for Cargo to complain that the function you're assigning from (for instance) doesn't match the declaration than for Rust to try to help you do the wrong thing, only for you to have to spend ages debugging elsewhere.
3. **match**  match was new to me, and I love it. It's not dissimilar to "switch" in other languages but is used extensively in Rust. It makes for legible code, and Cargo will have a good go at warning you if you do something foolish (such as miss possible cases). My general rule of thumb, where I'm managing different options or doing branching, is to ask whether I can use match. If I can, I will.
4. **mut**  When declaring a variable, if it's going to change after its initialisation, then you need to declare it mutable. A common mistake is to declare something mutable when it _isn't_ changed—but the compiler will warn you about that. If you get a warning from Cargo that a mutable variable isn't changed when you think it _is_, then you may wish to check the scope of the variable and make sure that you're using the right version.
5. **return**  I actually very rarely use return, which is for returning a value from a function, because it's usually simpler and clearer to read if you just provide the value (or the function providing the return value) at the end of the function as the last line. Warning: you _will_ forget to omit the semicolon at the end of this line on many occasions; if you do, the compiler won't be happy.
6. **unsafe**  This does what it says on the tin: If you want to do things where Rust can't guarantee memory safety, then you're going to need to use this keyword. I have absolutely no intention of declaring any of my Rust code unsafe now or at any point in the future; one of the reasons Rust is so friendly is because it stops this sort of hackery. If you really need to do this, think again, think yet again, and then redesign. Unless you're a seriously low-level systems programmer, _avoid_ unsafe.
7. **use**  When you want to use an item, e.g., struct, variable, function, etc. from another crate, then you need to declare it at the beginning of the block where you'll be using it. Another common mistake is to do this but fail to add the crate (preferably with a minimum version number) to the `Cargo.toml` file.
This isn't the most complicated article I've ever written, I know, but it's the sort of article I would have appreciated when I was starting to learn Rust. I plan to create similar articles on key functions and other Rust must-knows: let me know if you have any requests!
* * *
_This article was originally published on [Alice, Eve, and Bob][6] and is reprinted with the author's permission._
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/10/keywords-rust
作者:[Mike Bursell][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mikecamel
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rustacean-tshirt.jpg?itok=u7LBmyaj (Rustacean t-shirt)
[2]: https://www.rust-lang.org/
[3]: https://opensource.com/article/20/5/rust-java
[4]: https://doc.rust-lang.org/std/
[5]: https://doc.rust-lang.org/cargo/
[6]: https://aliceevebob.com/2020/09/01/rust-my-top-7-keywords/

View File

@ -0,0 +1,55 @@
[#]: collector: "lujun9972"
[#]: translator: "mcfd"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
[#]: subject: "My top 7 keywords in Rust"
[#]: via: "https://opensource.com/article/20/10/keywords-rust"
[#]: author: "Mike Bursell https://opensource.com/users/mikecamel"
在Rust中的前7个关键字
======
从Rust标准库学习一些有用的关键字。
![Rustacean t-shirt][1]
我使用 [Rust][2] 已经有几个月了,写的东西比我预期的要多——尽管很多已经废弃掉了,但随着我的学习,我改进了所写的代码,并完成了一些超出我最初意图的更复杂的任务。
我仍然喜欢它,并认为谈论一些在 Rust 中重复出现的重要关键字会很好。我将提供我个人的总结:为什么你需要考虑如何使用它们,以及任何其他有用的东西,特别是对于刚接触 Rust 的新手或来自另一种语言的人(如 Java请阅读我的文章 [*为什么作为一个 Java 程序员的我喜欢学习 Rust*](https://opensource.com/article/20/5/rust-java)).
事不宜迟,让我们开始吧。获取更多信息的好地方总是 Rust 官方文档——你可能想从[ std 标准库][4]开始.
1. **const** – 你可以用 const 声明常量。虽然这不是造火箭,但请一定要用 const ,如果你要在不同的模块中使用常量,那请创建一个 `lib.rs` 文件Rust 默认的)你可以把所有的常量放在一个命名良好的模块中。我曾经在不同模块的不同文件中发生过 const 变量名(和值)的冲突,仅仅是因为我太懒了,除了在不同文件中剪切和粘贴之外,我本可以通过创建一个共享模块来节省大量的工作。
2. **let** – 你并不 _总是_ 需要用 let 语句声明一个变量但当你这样做时你的代码会更加清晰。此外如果可以请一定要添加变量类型。Rust 编译器会尽最大努力猜测它应该是什么类型的变量,但它不一定总能在编译时做到这一点(在这种情况下,编译器 [Cargo][5] 会提示你), 它甚至可能做不到你期望的那样。在这种情况下,对于 Cargo 来说,抱怨你所赋值的函数(例如)与声明不一致,总比 Rust 试图帮助你做错事,而你却不得不在其他地方花费大量时间来进行调试要简单。
3. **match**  match 对我来说是新鲜事物,我喜欢使用它。它与其他编程语言中的 "switch" 没有什么不同,但在 Rust 中被广泛使用。它使代码更清晰易读如果你做了一些愚蠢的事情例如错过一些可能的情况Cargo 会很好地提示你。我一般的经验法则是,在管理不同的 option 或进行分支时,如果可以使用 match那就请一定要使用它。
4. **mut**  在声明一个变量时如果它的值在声明后会发生变化那么你需要声明它是可变的Rust 中变量默认是不可变的)。常见的错误是在某个变量 _没有_ 变化的情况下声明它是可变的,这时编译器会警告你。如果你收到了 Cargo 的警告,说一个可变的变量没有被改变,而你认为它被 _改变_ 了,那么你可能要检查该变量的范围,并确保你使用的是正确的版本。
5. **return** – 实际上我很少使用 return它用于从函数中返回一个值但是如果你只是在函数的最后一行提供值或提供返回值的函数通常会变得更简单能更清晰地阅读。警告在很多情况下_会_ 忘记省略这一行末尾的分号(;),如果你这样做,编译器会不高兴的。
6. **unsafe** – 如其意:如果你想做一些不能保证 Rust 内存安全的事情,那么你就需要使用这个关键字。我绝对无意在现在或将来的任何时候宣布我的任何 Rust 代码不安全Rust如此友好的原因之一是它阻止了这种黑客行为。如果你真的需要这样做再想想再想想然后重新设计代码。除非你是一个非常低级的系统程序员否则要 _避免_ 使用 unsafe。
7. **use** – 当你想使用另一个 crate 中的东西时,例如结构体、变量、函数等来自另一个 crate ,那么你需要在你要使用它的代码的代码块的开头声明它。另一个常见的错误是,你这样做了,但没有在 `Cargo.toml` 文件中添加该 crate (最好有一个版本号)。
我知道,这不是我写过的最复杂的文章,但这是我在开始学习 Rust 时会欣赏的那种文章。我计划在关键函数和其他 Rust 必知知识方面创建类似的文章:如果你有任何要求,请告诉我!
* * *
_本文最初发表于 [Alice, Eve, and Bob][6] 经作者许可转载。_
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/10/keywords-rust
作者:[Mike Bursell][a]
选题:[lujun9972][b]
译者:[mcfd](https://github.com/mcfd)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mikecamel
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rustacean-tshirt.jpg?itok=u7LBmyaj "Rustacean t-shirt"
[2]: https://www.rust-lang.org/
[3]: https://opensource.com/article/20/5/rust-java
[4]: https://doc.rust-lang.org/std/
[5]: https://doc.rust-lang.org/cargo/
[6]: https://aliceevebob.com/2020/09/01/rust-my-top-7-keywords/