- Rust Programming Cookbook
- Claus Matzinger
- 344字
- 2021-06-24 12:27:42
How it works...
This recipe played with several concepts, so let's unpack them here. After setting up a library to work with unit tests as our playground in step 1 to step 3, we create a first test to work on some built-in data types to go through the basics in step 4 and step 5. Since Rust is particularly picky about type conversions, the test applies some math functions on the outcomes and inputs of different types.
For experienced programmers, there is nothing new here, except for the fact that there is an overflow_sub() type operation that allows for overflowing operations. Other than that, Rust might be a bit more verbose thanks to the (intentional) lack of implicit casting. In step 5, we intentionally provoke an overflow, which leads to a runtime panic (and is the test result we are looking for).
As shown in step 5, Rust offers struct as the foundation for complex types, which can have attached implementation blocks as well as derived (#[derive(Clone, Copy, Debug)]) implementations (such as the Debug and Copy traits). In step 6, we go through using the type and its implications:
- No overhead on custom types: struct has exactly the size that the sum of its properties has
- Some operations implicitly invoke a trait implementation—such as the assignment operator or the Copy trait (which is essentially a shallow copy)
- Changing property values requires the mutability of the entire struct function
There are a few aspects that work like that because the default allocation strategy is to prefer the stack whenever possible (or if nothing else is mentioned). Therefore, a shallow copy of the data performs a copy of the actual data as opposed to a reference to it, which is what happens with heap allocations. In this case, Rust forces an explicit call to clone() so the data behind the reference is copied as well.
We've successfully learned how to create and use data types. Now, let's move on to the next recipe.
- 深度實(shí)踐OpenStack:基于Python的OpenStack組件開(kāi)發(fā)
- 案例式C語(yǔ)言程序設(shè)計(jì)
- C++程序設(shè)計(jì)(第3版)
- Mastering JavaScript Object-Oriented Programming
- 控糖控脂健康餐
- JavaScript 網(wǎng)頁(yè)編程從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開(kāi)發(fā)視頻大講堂)
- 深度強(qiáng)化學(xué)習(xí)算法與實(shí)踐:基于PyTorch的實(shí)現(xiàn)
- 概率成形編碼調(diào)制技術(shù)理論及應(yīng)用
- Python算法詳解
- Frank Kane's Taming Big Data with Apache Spark and Python
- GitHub入門與實(shí)踐
- 零基礎(chǔ)學(xué)HTML+CSS
- C++ System Programming Cookbook
- Magento 2 Beginners Guide
- iOS開(kāi)發(fā)項(xiàng)目化入門教程