- Rust Programming Cookbook
- Claus Matzinger
- 285字
- 2021-06-24 12:27:45
How it works...
Rust's documentation is very versatile and allows for variations on doctests that would not be possible to cover in a single recipe. However, the documentation of these tools is also excellent, so, for more details, check out https://doc.rust-lang.org/rustdoc/documentation-tests.html.
What we covered in this recipe is a great way to document structs and functions in your code by adding examples that will be compiled and run on every test run. Not only will these be helpful for your readers and regression testing, but they also require you to think about how the code works as a black box. These tests are executed whenever code (``` in a fence ```) is encountered in the Example section of the documentation. In step 2 and step 3, we create these examples and see the result in step 4 and step 10.
If you are now wondering how some documentation can show a fraction of the code required while it is supposed to be run, step 8 shows the resolution to this riddle: # can hide individual lines while executing them. However, sometimes the code is not executed at all, as step 5 shows. We can declare a section as ignore and this code won't be run (without any visual indication in the output).
Furthermore, these tests can fail just like any other test by panicking (which can be allowed as well) or falling through an assert! macro. All in all, by hiding away boilerplate or other non-essential code, the reader can focus on the important bits, while the test still covers everything.
We've successfully tested our documentation—we can sleep easy and move on to the next recipe.
- 從零開始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- Mastering Python Scripting for System Administrators
- 編寫高質(zhì)量代碼:改善Python程序的91個建議
- Raspberry Pi for Secret Agents(Third Edition)
- Koa開發(fā):入門、進(jìn)階與實(shí)戰(zhàn)
- Building Mapping Applications with QGIS
- Cassandra Data Modeling and Analysis
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- TypeScript項(xiàng)目開發(fā)實(shí)戰(zhàn)
- Qt 4開發(fā)實(shí)踐
- Apache Solr PHP Integration
- Learning Unreal Engine Game Development
- 數(shù)據(jù)科學(xué)中的實(shí)用統(tǒng)計(jì)學(xué)(第2版)
- Mastering ASP.NET Core 2.0
- Java服務(wù)端研發(fā)知識圖譜