这个enum的用法,比c要强,和GO类似。

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter,
}

fn value_in_cents(coin: Coin) -> u32 {
    match coin {
    Coin::Penny => {
        println!("Lucky penny!");

    },
    Coin::Nickel => ,
    Coin::Dime => ,
    Coin::Quarter => ,
    }
}

fn main() {
    let penny = value_in_cents(Coin::Penny);
    println!("The coin is {}.", penny);

    let nickel = value_in_cents(Coin::Nickel);
    println!("The coin is {}.", nickel);

    let dime = value_in_cents(Coin::Dime);
    println!("The coin is {}.", dime);

    let quarter = value_in_cents(Coin::Quarter);
    println!("The coin is {}.", quarter);
}

Rust中的枚举及模式匹配的更多相关文章

  1. 2.8 rust 枚举与模式匹配

    Enums and Pattern Matching 摘要 枚举定义 enum IpAddrKind { V4, V6, } 枚举方法 fn main() { enum Message { Quit, ...

  2. 【译】理解Rust中的Futures (一)

    原文标题:Understanding Futures In Rust -- Part 1 原文链接:https://www.viget.com/articles/understanding-futur ...

  3. 【译】理解Rust中的Futures(二)

    原文标题:Understanding Futures in Rust -- Part 2 原文链接:https://www.viget.com/articles/understanding-futur ...

  4. 【译】深入理解Rust中的生命周期

    原文标题:Understanding Rust Lifetimes 原文链接:https://medium.com/nearprotocol/understanding-rust-lifetimes- ...

  5. Rust 中的数据布局--可选的数据布局

    Rust 允许你指定不同于默认的数据布局策略,并为你提供了不安全代码指南. repr(C) 这是最重要的"repr".它的意图相当简单:做 C 所做的事.字段的顺序.大小和对齐方式 ...

  6. Rust 中的数据布局--非正常大小的类型

    非正常大小的类型 大多数的时候,我们期望类型在编译时能够有一个静态已知的非零大小,但这并不总是 Rust 的常态. Dynamically Sized Types (DSTs) Rust 支持动态大小 ...

  7. Rust 中的数据布局-repr

    repr(Rust) 首先,所有类型都有一个以字节为单位的对齐方式,一个类型的对齐方式指定了哪些地址可以用来存储该值.一个具有对齐方式n的值只能存储在n的倍数的地址上.所以对齐方式 2 意味着你必须存 ...

  8. Python 中的枚举类型~转

    Python 中的枚举类型 摘要: 枚举类型可以看作是一种标签或是一系列常量的集合,通常用于表示某些特定的有限集合,例如星期.月份.状态等. 枚举类型可以看作是一种标签或是一系列常量的集合,通常用于表 ...

  9. JavaScript中的枚举

    在JavaScript目前的版本中,没有枚举这个概念(当然,ECMA-262第三版中已经将enum作为关键字保留). 然而,如同JavaScript中没有class一样,但我们仍然可以通过间接的方式- ...

随机推荐

  1. day46_9_5前端(3)

    一.调节长宽. 在css中可以对块级标签设置长和宽,但是对行内标签无效,其属性如下: 1.height:80px 高度. 2.width:80px 宽度. 二.字体属性. 设置一个标签中的字体.比如黑 ...

  2. golang 服务端和客户端(二)

    1.golang服务端 package main import( "net/http" ) func main(){ //注册处理函数,用户连接,自动调用指定的处理函数 http. ...

  3. 《为什么说Redis是单线程的以及Redis为什么这么快!》

    为什么说Redis是单线程的以及Redis为什么这么快!   一.前言 近乎所有与Java相关的面试都会问到缓存的问题,基础一点的会问到什么是“二八定律”.什么是“热数据和冷数据”,复杂一点的会问到缓 ...

  4. Python 基础排序算法

    冒泡排序(bubble sort) 思路 以升序为例: 从第一个数开始向后两两对比,将大的数一直向后移动,直至最大的数移到最后,再找第二大的数 最好情况:O(n) 一般情况:O(n^2) 最坏情况:O ...

  5. Centos7防火墙添加端口

    添加 firewall-cmd --zone=public --add-port=80/tcp --permanent   (--permanent永久生效,没有此参数重启后失效) 重新载入 fire ...

  6. AGC037C Numbers on a Circle(神奇思路)

    Atcoder 全是神仙题-- 先变成能不能从 \(b\) 到 \(a\).操作变成一个数减掉旁边两个数. 考虑里面最大的且不和 \(a\) 中相等的那个数.它两边的数此时都不能操作,否则就减到非正数 ...

  7. 'GL_EXT_shader_framebuffer_fetch' : extension is not supported

    在使用安卓模拟器加载Flutter应用时, 提示'GL_EXT_shader_framebuffer_fetch' : extension is not supported: D/skia (1404 ...

  8. Python 将numpy array由浮点型转换为整型

    Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:

  9. Missing artifact com.lowagie:itextasian:jar:2.1.7

    在maven中添加iTextAsian支持   导入maven项目 发现pom.xml文件 中这个依赖报错:Missing artifact com.lowagie:itextasian:jar:2. ...

  10. Prometheus 监控K8S集群资源监控

    Prometheus 监控K8S集群中Pod 目前cAdvisor集成到了kubelet组件内,可以在kubernetes集群中每个启动了kubelet的节点使用cAdvisor提供的metrics接 ...