首页 > 其他分享 >[Rust] Intro Enum

[Rust] Intro Enum

时间:2024-02-12 16:11:06浏览次数:20  
标签:foo Bar enum Enum value Intro Foo Rust

Go Enum

package main

type GoEnum = int
const (
    Foo GoEnum = iota
    Bar
    Baz
)

func main() {
}

 

Typescript Enum

enum TsEnum {
    Foo,
    Bar,
    Baz
}

 

Rust Enum

enum RsEnum {
    Foo,
    Bar,
    Baz
}

Why go over enums...

They are simple constructs. But you can add types to Rust Enum.

enum TsEnum {
    Foo(i32),
    Bar(String),
    Baz(Vec<String>)
}

Example:

enum TsEnum {
    Foo(i32),
    Fuu(Option<i32>),
    Bar(String),
    Baz(Vec<String>)
}

fn main() {
    // set value 5
    let foo = RsEnum::Foo(5);
    // pattern matching, pick the value
    if let RsEunm::Foo(value) = foo {
       
    }
    // or you can also do
    match foo {
       RsEnum::Foo(value) => {},
       RsEnum::Fuu(Some(value)) => { /* Has value */ },
       RsEnum::Fuu(None) => { /* Doesn't has value */ },
       _ => { /* Cover the rest cases */ }
    }
}

 

We can also do generic on them

enum Foo<T> {
    Bar(T)
}

 

How is this paractically useful?

  1. lists with many types
  2. Nullable
  3. Error Handling

Example of nullable in TS

type Foo = {
    bar?: string
}

function doSomething(foo: Foo): boolean {
   if (foo.bar) {
      return true
   } else {
      // ...
      return false
   }
}

in Rust

enum Option<T> {
    None,
    Some(T)
}

fn main () {
    let foo = Some(5)
    
    if let Some(value) = foo {
       // then value is i32
    }
    
    match foo {
        Some(value) => { /* value is i32*/ },
        None => {} 
    }
    
    // the same as in TS: foo.amp((x) => {})
    // do the transform fn only when there is a value
    foo.map(|x| => {
      
    })
    
    foo.filter(|&x| x < 10)
    
    if foo.is_some() {
       let sum = foo.unwrap() + 5;
    };
    
    foo.unwrap_or(0);
    
    foo.unwrap_or_else(|| {
        return 5
    })
}

 

标签:foo,Bar,enum,Enum,value,Intro,Foo,Rust
From: https://www.cnblogs.com/Answer1215/p/18013951

相关文章

  • IfcChimneyTypeEnum
    IfcChimneyTypeEnum类型定义此枚举定义了可以使用枚举值预定义的烟囱的有效类型。 IFC4中的新枚举注:目前没有定义特定的枚举器,已添加IfcChimneyTypeEnum以供将来扩展。 EnumerationdefinitionConstantDescriptionUSERDEFINED NOTDEFINED   EXPRESS......
  • extism 基于rust 开发的强大webassembly 框架
    extism基于rust开发的强大webassembly框架包含的特性使用简单 可以方便的开发基于webassembly的插件系统安全方便运行 包含了灵活的架构可以可以方便与多种语言进行通信(基本覆盖了主流的编程语言)说明目前基于webassembly的语言集成热度是越来越高了,webassembly很值......
  • IfcCoveringTypeEnum
    IfcCoveringTypeEnum类型定义此枚举定义了可以进一步指定IfcOvering或IfcOveringType的不同覆盖类型的范围。 IFC1.0中的新枚举IFC4添加了以下枚举器MOLDING和SKITINGBOARD。 EnumerationdefinitionConstantDescriptionCEILINGThecoveringisusedtorepresenta......
  • IfcDoorTypeOperationEnum
    IfcDoorTypeOperationEnum类型定义该枚举定义了描述门如何操作的基本方法,如图180所示。它将车门划分为单个或多个门板以及这些门板的操作类型。 在最常见的摆动门情况下,IfcDoorTypeOperationEnum定义了铰链侧(左悬或右悬)和打开方向(向左打开,向右打开)。门是向内打开还是向外打开......
  • IfcRailingTypeEnum
    IfcRailingTypeEnum类型定义此枚举定义了可以使用枚举值预定义的IfcRailing或IfcRailingType的不同类型。 IFC2.0中的新枚举类型 EnumerationdefinitionConstantDescriptionHANDRAILAtypeofrailingdesignedtoserveasanoptionalstructuralsupportforlo......
  • 48从零开始用Rust编写nginx,搭建一个简单又好看官方网站
    wmproxywmproxy已用Rust实现http/https代理,socks5代理,反向代理,负载均衡,静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实现过程分享出来,感兴趣的可以一起造个轮子项目地址国内:https://gitee.com/tickbh/wmproxygithub:https://github.com/tickbh/wmpro......
  • introduction to dds
    DataDistributionServiceTosolvetheproblemwhenmassivedataisacquiredtobedistributedReal-time,efficiently,flexibly.Dataisthecenter.AdaptiveAUTOSARisthefirstcompanythatappliedDDSasoneoftheoptionalcommunicationmethods.ROS2......
  • IfcRampFlightTypeEnum
    IfcRampFlightTypeEnum类型定义此枚举定义IfcRampFlight或IfcRampFlightType对象可以实现的不同类型。 IFC2x2中的新枚举。 EnumerationdefinitionConstantDescriptionSTRAIGHTArampflightwithastraightwalkingline.SPIRALArampflightwithacircul......
  • IfcDoorTypeEnum
    IfcDoorTypeEnum此枚举定义IfcDoor或IfcDoorType对象的不同预定义类型。 IFC4中的新枚举。 EnumerationdefinitionConstantDescriptionDOORAstandarddoorusuallywithinawallopening,asadoorpanelinacurtainwall,orasa"freestanding"door.GA......
  • rust使用proxy-wasm-rust-sdk开发envoy wasm 进行GRPC调用
    在一些业务中,对于客户端发送的请求,需要调用grcp服务来确认是否合规,这个时候可以在入口网关做些统一的处理。之前写的用go来编写wasm,在编写grpc调用时发现由于tinygo的原因导致无法进行grpc请求,在找了一圈后决定使用proxy-wasm-rust-sdk来完成该部分功能。一、创建项目cargone......