rust初学

1.变量默认不可变

2.宏使用感叹号调用

let x=1;
println!("{:?}",x); //:?调试模式

3.if else

let x=10;
if x>10 {
    println!("big");
}else{
    println!("small");
}

4.循环

let mut x=0;
loop{
    if x==5 {
        break;
    }
    println!("{:?}",x);
    x=x+1;
}
//0
//1
//2
//3
//4
let mut x=0;
while x!=5 {
    println!("{:?}",x);
    x=x+1;
}

5.函数

fn main() {
    first_name();//first
    last_name(666);//666
    println!("{}", add(1,2));//3
}
 
fn first_name(){
    println!("first");
}
 
fn last_name(last:i32){
    println!("{}",last);
}
 
fn add(a:i32, b:i32)->i32{
    a+b
}

6.match

fn main() {
    let fuck=true;
    match fuck{
        true=>println!("true"),
        false=>println!("false"),
    }//true
    let some_int=5;
    match some_int{
        1=>println!("one"),
        2=>println!("two"),
        _=>println!("else"),
    }//else
}

 match编译器会检查,所有可能性,if else不会

7.枚举

fn main() {
    enum Fuck {
        Up,
        Down,
    } //枚举类型
    fn which_way(go: Fuck){
        match go {
            Fuck::Up => println!("up"), 
            Fuck::Down => println!("down"),
        } //枚举值
    }
    which_way(Fuck::Up); //up
    which_way(Fuck::Down); //down
}

8.结构体

fn main() {    
    struct Fuckbox{
        depth: i32,
        width: i32,
        height: i32,
    }
    let fuck=Fuckbox{
        depth: 10,
        width: 20,
        height: 30,
    };
    println!("{}",fuck.depth); //10
    println!("{}",fuck.width); //20
    println!("{}",fuck.height); //30
}

9.元组

fn main() {
    fn one_two_three()->(i32,i32,i32){
        (1,2,3)
    }
    let (a,b,c) = one_two_three();
    let numbers=one_two_three();
    println!("{:?},{:?}",a,numbers.0); //1,1
    println!("{:?},{:?}",b,numbers.1); //2,2
    println!("{:?},{:?}",c,numbers.2); //3,3
}

10.表达式

fn main() {
    let x=3;
    let to_five=if x<5 { 
        true 
    } else { 
        false
    };
    println!("{}", to_five); //true
    let to_5=x<5;
    println!("{}", to_5); //true
}
fn main() {
    let x=2;
    let message=match x{
        1=>"one",
        _=>"else",
    };
    println!("{}",message);//else
}

11.借用

enum Light{
    Dull,
}
fn display_light(light:&Light){
    match light{
        Light::Dull => println!("dull"),
    }
}
fn main() {
    let dull = Light::Dull;
    display_light(&dull);//dull
    //不借用的话,下面无法执行,因为所有权被转移
    display_light(&dull);//dull
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值