DEV Community

Zhangwuji
Zhangwuji

Posted on

rust2

1.

struct animal {
   name:String
   age:u8
}

impl animal {
    // static function
    fn create(name:String,age:u8) -> animal {
         return {
            name:'jack',
            age:20
          }
    }
    // instance function
    fn say(&self){

    }


}

let animalInstance:animal = {
   name:"大灰狼",
   age:20
};
animalInstance.say();

Enter fullscreen mode Exit fullscreen mode

enumerate Option
it'appearance solve the problem of null point.

let s = some(2);
let s2 = some(20);

if let some(x) = s {
    x == 2 //true
}else{
  None
};

match s {
   some(x) => {
      x == 2
   },
   (_) => {
     println!("我是none")
   }
};

match (s,s2) {
   (Some(s),Some(s2)) => {
     s == 2 // true
     s2 == 20 // true
   },
   (_,(Some(S2))) => {

  }
}

let x =0;
let y=1;

match(x,y){
   (0,1) =>{

   },
   (_,1)=>{

   }

};
Enter fullscreen mode Exit fullscreen mode

package mechanism

  // lib.ts 
  // mod match2 {
      pub fn add(x:i8,y:i8) -> i32 {
          x+y
      }
   }

 // main.ts
 use  guessing_game::math2;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)