美文网首页
Rust,练练代码

Rust,练练代码

作者: 万州客 | 来源:发表于2022-08-22 16:43 被阅读0次

    下载了一本书,练练。

    2022-08-23 16_42_48-.png

    一,代码

    static G1: i32 = 3;
    
    type Age = u32;
    
    fn grow(age: Age, year: u32) -> Age {
        age + year
    }
    
    fn main() {
        /*
        println!("Hello, world!");
        println!("{}", 1);
        println!("{:o}", 9);
        println!("{:x}", 255);
        println!("{:X}", 255);
        println!("{:p}", &0);
        println!("{:b}", 15);
        println!("{:e}", 10000f32);
        println!("{:E}", 10000f32);
        println!("{:?}", "test");
        println!("{:#?}", ("test1", "test2"));
        println!("{a} {b} {b}", a = "x", b = "y");
        let _variable: i32 = 100;
        let x = 5;
        x = 10;
        let mut x = 5;
        x = 10;
        let (mut a, mut b) = (1, 2);
        let x = "hello";
        println!("x is {}", x);
        let x = 5;
        println!("x is {}", x);
        let mut v = Vec::new();
        v.push(1);
        v.push(2);
        v.push(3);
    
        let v = v;
        for i in &v {
            println!("{}", i);
        }
        let elem = 5u8;
        let mut vec = Vec::new();
        vec.push(elem);
        println!("{:?}", vec);
        let player_scores = [
            ("Jack", 20), ("Jane", 23), ("Jill", 18), ("John", 19),
        ];
    
        let players:Vec<_> = player_scores
            .iter()
            .map(|&(player, _score)| {
                player
            })
            .collect();
        println!("{:?}", players);*/
        let x: Age = 20;
        println!("20 year later: {}", grow(x, 20));
        println!("{}", G1);
    }
    
    

    二,截图

    C:/Users/ccc/.cargo/bin/cargo.exe run --color=always --package first_rust --bin first_rust
       Compiling first_rust v0.1.0 (D:\tmp\first_rust)
        Finished dev [unoptimized + debuginfo] target(s) in 0.48s
         Running `target\debug\first_rust.exe`
    20 year later: 40
    3
    
    Process finished with exit code 0
    
    2022-08-23 16_40_42-MessageCenterUI.png

    相关文章

      网友评论

          本文标题:Rust,练练代码

          本文链接:https://www.haomeiwen.com/subject/bzyggrtx.html