美文网首页
20240312近期rust学习笔记和代码3

20240312近期rust学习笔记和代码3

作者: 李荣强Justin | 来源:发表于2024-03-11 17:02 被阅读0次

    公共枚举的变量都是默认公共的

    Vector用的比较多,所以有预导入prelude机制,而hashmap用的比较少,不在prelude中

    hashmap 堆内存,同构造

    collect特定类型

    hashmap/迭代

    还是要多写代码,多写代码才能学好编程

    panic! 打印错误信息,展开和清理调用栈,并中止执行,默认情况下,展开调用栈。(工作量大)

    可以把默认展开改为中止

    [profile.release] 生产环境

    panice ="abort"

    result 枚举:  Ok Err

    use std::collections::HashMap;

    fn main() {

        println!("Hello, world!");

        let s1 = String::from(" eth is good");

        let s2 = String::from(" eth will up to 10000");

        println!("{}{}", s1, s2);

        let s3 = s1 + &s2;

        println!("{}", s3);

        println!("{}", s2);

        //println!("{}", s1);

        let s4 = String::from(" realms is good");

        let s5 = String::from(" realms will up to 10000");

        let s = format!("{}--{}", s4, s5);

        println!("{}", s);

        let  tokens:Vec<String> = vec![String::from("BTC"),String::from("ETH"),String::from("APX")];

        let  amounts:Vec<f64> = vec![0.0012,1200000.345,167098.1];

        let holdings: HashMap<&String,&f64> = tokens.iter().zip(amounts.iter()).collect();

        println!("{:?}",holdings);

        let mut another_hold = HashMap::new();//必须为mut关键词修饰的,不然不能插入

        another_hold.insert(String::from("SSV"),12345.67);

        println!("{:?}",another_hold);//{:?} 完整输出

        let mut layer2_tvl:HashMap<String,f64> = HashMap::new();

        layer2_tvl.insert(String::from("ARB"),30.15);

        layer2_tvl.insert(String::from("OP"),20.14);//

        layer2_tvl.insert(String::from("OP"),18.14);//同样的key 会覆盖原来的value

        let op_tvl = layer2_tvl.get("OP");

        match op_tvl {

            Some(tvl) => println!("OP TVL is {}",tvl),

            None => println!("OP TVL is not found"),

        }

        layer2_tvl.entry(String::from("STARK")).or_insert(3.105);

        for (layer2,tvl) in &layer2_tvl {

            println!("{}:{}",layer2,tvl);

        }

        let v1 = vec![1,2,3];

        println!("{}",&v1[5]);

        panic!("crash and burn");

        layer2_tvl.entry(String::from("STARK")).or_insert(13.105);

        let stark_tvl = layer2_tvl.get("STARK");

        match stark_tvl {

            Some(tvl) => println!("STARK TVL is {}",tvl),

            None => println!("STARK TVL is not found"),

        }

    }

    相关文章

      网友评论

          本文标题:20240312近期rust学习笔记和代码3

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