• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Basic Grammar 2

Because Rust grammar is quite different from most languages, learn grammar slowly

tuples

  • tuple
    • If I write it like thislet aa = (1i32,true);
    • Is for the first one isi32Type. The second one is a bool
    • Let’s look at the following code
fn main() {
    let p = (1i32.2i32);
    let (a , b) = p;
    let x = p.0;
    let y = p.1;

    // Output 1, 2, 1, 2
    println!("{} {} {} {}",a,b,x,y);
}
Copy the code
  • Among thema,bThe assigned value is usedPattern matching
    • And x and y are assigned using a numeric index

The enumeration

  • Generally reading enumerations uses pattern matching
use std::num::NonZeroU8;

enum Number {
    Int(i32),
    Float(f32)}fn input_num(num : &Number) {
    match num {
        &Number::Int(v) => println!("Integer {}",v),
        &Number::Float(v) => println!("Float {}",v),
    }
}

fn main() {

    let n : Number = Number::Int(100);
    // Output Integer 100
    input_num(&n);
}
Copy the code

Basic arithmetic expressions

fn main() {

    let x = 100;
    let y = 10;
    // 110 90 1000 100
    println!("{} {} {} {} {}", x + y, x - y, x * y, x / y, x % y);
}
Copy the code

Code minor

  • If a continuous equal judgment occurs, parentheses need to be added semantically as appropriate
    • If the following code,a==b==cWithout parentheses, the compiler will report an error
fn calculate(a: bool,b: bool,c: bool) - >bool {
    (a == b) == c
}

fn main() {

    // true
    println!("{}",calculate(false.true.false));
}
Copy the code

if-else

fn func(i: i32) {
    if i > 10 && i < 20 {
        print!("i > 10");
    }else if i > 20 && i < 30 {
        print!("i > 20")}else {
        print!("i > 30")}}fn main() {
    // i > 30
    func(40);
}
Copy the code

function

fn add(t : (i32.i32)) - >i32 {
    t.0 + t.1
}

fn main() {
    3 / / output
    println!("{}",add((1.2)))}Copy the code
  • A variant of the above function
fn add((x,y) : (i32.i32)) - >i32 {
    x + y
}

fn main() {
    3 / / output
    println!("{}",add((1.2)))}Copy the code
  • When a type conversion occurs to a function
    • Add1 is passing in a parameter
    • Add2 is passing in a tuple, two tuplesi32Type variable
    • The following code is equivalent tofuncA type conversion is performed
fn add1(t : (i32.i32)) - >i32 {
    t.0 + t.1
}

fn add2((x,y) : (i32.i32)) - >i32 {
    x + y
}

fn add3(x: i32,y: i32) - >i32 {
    x + y
}

fn main() {
    let mut func : fn((i32.i32)) - >i32 = add1;
    func = add2;
    / / output 7
    println!("{}",func((2.5)))}Copy the code

Conditional judgment fit function

fn check(x : bool) - >i32 {
    let p = if x {
        panic!("Error!");
    } else {
        7
    };
    return p;
}

fn main() {
    let func = check(false);
    / / output 7
    println!("{}",func)
}
Copy the code

Fibonacci of recursion

fn fib(i : u32) - >u64 {
    if i == 1 || i == 2 {
        return 1
    } else {
     return fib(i - 1) + fib(i - 2); }}fn main() {
    let c = fib(10);
    / / output 55
    println!("{}",c)
}
Copy the code

trait

trait Shape { fn area(self: &Self) -> f64; } struct Circle { r: f64, } impl Shape for Circle { fn area(self: &Self) -> f64 { std::f64::consts::PI * self.r * self.r } } fn main() { let c = Circle {r : 3f64}; // Output circle area is 28.274333882308138 println! (" Circle area is {}",c. rea())}Copy the code
  • As you can see in the code above
    • If I have oneCircleType c, we can call internalc.area()
    • Can be achieved byself.rTo access internal members