小编Nav*_*dat的帖子

匹配内部可变枚举的预期方法是什么?

这些是我可以想出的方法来尝试匹配引用计数的内部可变枚举:

#![allow(unused)]

use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug, Clone, PartialEq)]
struct Bar {
    some_bar: Vec<f64>,
}
#[derive(Debug, Clone, PartialEq)]
struct Baz {
    some_baz: i32,
}

#[derive(Debug, Clone, PartialEq)]
enum Foo {
    Bar(Bar),
    Baz(Baz),
}

fn is_baz(foo_ref: Rc<RefCell<Foo>>) -> bool {
    match foo_ref {
        Foo::Bar(_) => false,
        Foo::Baz(_) => true,
    }
}

fn is_baz_borrow(foo_ref: Rc<RefCell<Foo>>) -> bool {
    match foo_ref.borrow() {
        Foo::Bar(_) => false,
        Foo::Baz(_) => true,
    }
}

fn is_baz_deref(foo_ref: Rc<RefCell<Foo>>) -> bool {
    match *foo_ref {
        Foo::Bar(_) => false,
        Foo::Baz(_) …
Run Code Online (Sandbox Code Playgroud)

enums matching rust interior-mutability

5
推荐指数
1
解决办法
1529
查看次数

标签 统计

enums ×1

interior-mutability ×1

matching ×1

rust ×1