- 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; - 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()
; - When the return value is an exclamation point
!
, it means that it will never return. The exclamation point can be regarded as a type hereNever
, and the function will either never return or must crash; - 3.2 Use
struct
keywords to define a complex type, usederive
keywords to let the compiler generate certain properties, andvec!
macros appear to quickly initialize aVec<u8>
type; - Figure 3.1 shows a memory distribution diagram of a custom structure. It can be seen from the figure that the
String
type 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 similarVec<u8>
partsString
;
- There are two ways to give a new structure a name, one is
struct
a keyword, and the other istype
a keyword, whichtype
is usually used to alias a certain type (alias), ortrait
specify an associated type (associate type) in a feature, see associated-items for details ; listing 3.3
A series of file IO APIs are given, in which typesString::from_utf8_lossy(&buffer)
can beVec<u8>
converted intoString
types;- 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;
- 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
self
notself
; - In Section 3.4, there is a
unsafe
scene 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; const
let
The difference between and ,let
the bound object has internal mutability, such asstd::sync::Arc
andstd::rc::Rc
, the value of its internal reference count will be changed through operations such as clone, from the perspective of the compiler,let
more emphasis is placed on aliasing a piece of data rather than its immutability (immutability);- Sections 3.4 and 3.5 mainly talk
enum
about handling error types, which are relatively simple; - Classes that implement
PartialEq
features can be==
used for comparison;Display
classes that implement features can be used for{}
structured output in println!,Display
features require implementationfmt(&self, f: &mut fmt::Formatter)
methods, andwrite!
macros can be used to simplify the implementation process; - Section 3.7 talks about the visibility of classes and members. The default is Private. Adding a
pub
keyword 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; - 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 similarrustdoc xx.rs
to the previous onecargo doc
, but cargo generates documentation for the entire project; ///
The generated document supports markdown syntax
Rust in Action Notes Chapter 3 Composite Data Types
Guess you like
Origin blog.csdn.net/Mint2yx4/article/details/131008467
Recommended
Ranking