Rust in Action Notes Chapter 3 Composite Data Types

  1. Pass #[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZI50wIh1-1685693144796)(null)]),不带感叹号的#[allow] 仅为下一行代码提供属性,属性有多种分类,文中出现的allow 属性称为诊断属性(Diagnostics),详见[诊断属性](https://doc.rust-lang.org/reference/attributes/diagnostics.html#lint-check-attributes);诊断属性还包括warn forbid deprecated must_use` etc;
  2. The unit type is a zero-length tuple with a length of 0, represented by a pair of parentheses, which is returned ()when the function has no return type (), or ;when the function ends with a semicolon, the type is also implicitly returned ();
  3. When the return value is an exclamation point !, it means that it will never return. The exclamation point can be regarded as a type here Never, and the function will either never return or must crash;
  4. 3.2 Use structkeywords to define a complex type, use derivekeywords to let the compiler generate certain properties, and vec!macros appear to quickly initialize a Vec<u8>type;
  5. Figure 3.1 shows a memory distribution diagram of a custom structure. It can be seen from the figure that the Stringtype consists of three parts. The first part is (ptr) a pointer to the heap, the second part (size) represents the size, that is, the length of the string, and the third part (capacity) represents the capacity, which represents the maximum capacity allocated for this string on the heap. If the subsequent string continues to increase, space may be re-allocated on the heap. The type has three similar Vec<u8>parts String;
    3.1
  6. There are two ways to give a new structure a name, one is structa keyword, and the other is typea keyword, which typeis usually used to alias a certain type (alias), or traitspecify an associated type (associate type) in a feature, see associated-items for details ;
  7. listing 3.3A series of file IO APIs are given, in which types String::from_utf8_lossy(&buffer)can be Vec<u8>converted into Stringtypes;
  8. Section 3.3 explains the difference between rust and classes in other languages. As shown in the figure, most languages ​​that support inheritance will put the methods and members of the class together, while rust is implemented separately, so classes in rust do not support the inheritance properties of object-oriented languages;3.2
  9. The note on page 84 briefly discusses the difference between a function (function) and a method (method). The author believes that a function is pure (pure), its behavior is determined only by its parameters, and a method is usually bound to a class, the first parameter is or correspondingly borrowed, but there are exceptions, for example, the first parameter of a class that implements a static method (static method) is selfnot self;
  10. In Section 3.4, there is a unsafescene where keywords are used, that is, when modifying static global variables, they must be wrapped with unsafe to avoid modifying a global variable in multiple places at the same time. Global variables should be all capitalized according to the habit;
  11. constletThe difference between and , letthe bound object has internal mutability, such as std::sync::Arcand std::rc::Rc, the value of its internal reference count will be changed through operations such as clone, from the perspective of the compiler, letmore emphasis is placed on aliasing a piece of data rather than its immutability (immutability);
  12. Sections 3.4 and 3.5 mainly talk enumabout handling error types, which are relatively simple;
  13. Classes that implement PartialEqfeatures can be ==used for comparison; Displayclasses that implement features can be used for {}structured output in println!, Displayfeatures require implementation fmt(&self, f: &mut fmt::Formatter)methods, and write!macros can be used to simplify the implementation process;
  14. Section 3.7 talks about the visibility of classes and members. The default is Private. Adding a pubkeyword to a class indicates that the class is public, but its members are still private. If you need members to be visible, you need to add pub in front of the members;
  15. Section 3.8 talks about the method of adding documentation to the code. It ///only annotates the subsequent code, //!but annotates the entire module. You can compare the previous #![allow]and horizontally. #[allow]The method of generating annotations is similar rustdoc xx.rsto the previous one cargo doc, but cargo generates documentation for the entire project;
  16. ///The generated document supports markdown syntax

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/131008467