【Rust日报】 2019-05-31:rust.cc社区提供了国内crates镜像

本文介绍了Rust中的Enum使用技巧,如何从Rust调用Java代码,提到了Tarpaulin代码覆盖率工具,以及rust.cc社区提供的国内crates镜像。还涵盖了Rust与Blender的结合、WASM游戏开发以及Rust调用Go库的案例。
摘要由CSDN通过智能技术生成

Enum的值如何作为类型

這位寫C++的老兄想寫以前的Enum

fn pet(_: Animal::Whale) {}

fn pet(_: Animal::Dog) {}

// or somehow describe a trait that forces its implementors to impl specific enum variants; not allow them to impl the whole enum

trait Petter<T> {

fn pet(_: Animal::T);

}

rust的近似方案1

struct Dog {}
struct Whale {}
enum Animal {
    Dog(Dog),
    Whale(Whale),
}

rust的近似方案2

// given there is a trait Bar, and types A and B implement it:
enum Foo {
    A(A),
    B(B),
}

impl Bar for Foo {
    fn bar(&self) {
        match self {
            A(a) => a.bar(),
            B(b) => b.bar(),
        }
    }
}

impl From<A> for Foo {
    fn from(a: A) -> Foo {
        Foo::A(a)
    }
}

有個庫enum_dispatch可以達到類似的效果


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值