Variable declarations let v1: i32 = 10; rust officials say it has many advantages, in fact, is to facilitate the development compiler programmer. .
let define constants. The variable constant need to add mut.
let defined variables can be repeated an arbitrary type
let x = 1 let x = true
Why did not the previous statement defined types, this function like c ++, like the auto, automatically derived type.
Alias definition display
type tInt = u32; let x2 :tInt = 20;
Static variables
Static variables declared its period of duration of the program is running
Note that the definition of
- Must be initialized immediately
- The compiler must believe that returns the constant initialization
- Plus mut allowed but must use unsafe
static AAA:i32 = 0; println!("{:?}", AAA); static mut AAA2 :i32 = 1; unsafe{ AAA2 = 21; println!("{:?}", AAA2); }
const define constants: const G2: i32 = 0;
Common data types and other language lacks a big difference
bool
char instead of one byte 4 bytes. Requires the use of an array of bytes may be used u8
I8 u8 p16 u16 i32 u32 i64 u64 128 128 help help
Integer overflow, rust proposed solutions
- checked_ * return None
- saturating_ * Returns the maximum value
- wrapping_ * Returns the truncated overflow
f32 f64
Float may return particular values need to pay attention to, ah
enum FpCategory{
Nan, sky
Infinite, infinite
Zero,0
Subnormal, decimal digits convergence of circumstances beyond
Normal Normal
}
These types of smart pointers and pointer later date
Box <T> has ownership, power to release memory
& T have no right to release the memory, have no right to write
& Mut T power to release memory, have the right to write
* Const T pointer read-only, no right to write
* Mut T have the right to write
Rec <T> share ownership, thread safe
Act <T> atoms reference pointer type, shared ownership, security thread
Cow <a, T> write replication
Type conversion is not supported implicit conversion, this new language has an advantage, use as, generally follow the standard library conversion.
Composite type
tuple tuple
let tup1 = (2131i32, "abc"); println!("{} {}", tup1.0, tup1.1);
A little amazing is allowed to define the empty tuple let ept :() = () take up memory space 0
struct structure
1 struct Poinit{ 2 x :i32, 3 y :i32, 4 } 5 let p = Poinit{x:1, y:2};
Also free structure struct AA; struct AA2 (); struct {};
type struct structure, a structure with no name
struct AA(i32,i32)
struct Age(i32); let age2 = Age(1); println!("{:?}", age2.0);
enum mainly used together with a matching pattern match
Type recursive definition, it can not contain itself.
Similar c
type struct Node{ void *data; struct Node *next; } struct Rex{ data:i32, next:Box<Rex>, }