Variable constant

let x = Hello world.; // declare immutable
let mut x; // Shadow x can be declared as different types
x = 1; // Assign and infer that the type is i64
const Y: bool = true & false; // The constant Y is visible throughout the scope
{
  let x = x; 
  // x = 2; // Error, frozen X is unvariable
}
x = 3; // The x is unsealed
Copy the code

The data type

scalar

// 1
let x: i128 = 98 _222;
let x: u128 = 0xff;
let x: isize = 0o77;
let x: usize = 0b1111_0000;
let x: u8 = b'A';
// 2
let x: f64 = 2.0; 
let y: f32 = 3.0;
Boolean. / / 3
let t = true;
let f: bool = false;
/ / 4 characters
let c = 'z';
let z = 'ℤ';
let heart_eyed_cat: char = '😻';
Copy the code

vector

// Tuple
let tup: (i32.f64.u8) = (500.6.4.1);
let tup = (500.6.4.1);
let (x, y, z) = tup;
let five_hundred = x.0;
/ / array
let a: [i32; 5] = [1.2.3.4.5];
let a = [3; 5];
let first = a[0];
Copy the code

function

fn main() {
  let x = plus_one(5);
  println!("The value of x is: {}", x);
} 
fn plus_one(x: i32) - >i32 {
  x + 1 // Non-statement expression without semicolon, meaning return value
}
Copy the code

Control structure

// if expression
let number = 3;
if number < 5 {
  println!("condition was true");
} else if number < 8 {
  println!("condition was false");
} else {
  //
}
let condition = true;
 // if expression, each branch must return a compatible type
let number = if condition { 5 } else { 6 };
// the loop expression
let mut counter = 0;
let result = loop {
  counter += 1;
  if counter == 10 {
    break counter * 2; / / the return value}};// while
let mut number = 3;
whilenumber ! =0 {
  println!("{}!", number); 
  number -= 1;
}
// for 
let a = [10.20.30.40.50];
for element in a.iter() {
  println!("the value is: {}", element);
}
// range
for number in (1.4).rev() {
  println!("{}!", number);
}
Copy the code

The ownership of

String

let s = String::from("hello");
let mut s = String::from("hello");
s.push_str(", world!");
Copy the code

The ownership of

let x = 5;
let y = x;
// both x and y exist together and have the same value
let s1 = String::from("hello");
let s2 = s1; // Heap data ownership is transferred, s1 no longer exists
let s3 = s2.clone(); // Clone Heap data
Copy the code

Quotation and borrowing

fn main() {
  let s1 = String::from("hello");
  let len = calculate_length(&s1);
  println!("The length of '{}' is {}.", s1, len);

  let mut s1 = String::from("hello");
  change(&mut s1);
  let len = calculate_length(&s1);
  println!("The length of '{}' is {}.", s1, len);
}
fn calculate_length(s: &String) - >usize {
  s.len()
}
fn change(some_string: &mut String) {
  some_string.push_str(", world");
}
Copy the code
  1. There can be multiple constant references but only one variable reference
  2. A constant reference and a mutable reference cannot coexist
// Hack multiple mutable references
let mut s = String::from("hello"); 
{ 
  let r1 = &mut s;
} R1 is no longer in scope
let r2 = &mut s;

// the scope of the reference is introduced -> lastused
let mut s = String::from("hello"); 
let r1 = &s; // no problem let r2 = &s; // no problem 
println!("{} and {}", r1, r2); // R1 and R2 are consumed
let r3 = &mut s; // no problem println! ("{}", r3);
Copy the code

Dangling pointer

fn main() {
  let reference_to_nothing = dangle();
} 
fn dangle() - > &String {
  let s = String::from("hello"); 
  &s
  // The reference returned when s is freed at the end of the function is invalid
}
Copy the code