小编JNP*_*JNP的帖子

[Rust Enums]:如何从 Rust 中的混合类型枚举中获取数据值?

你好,我是 Rust 的新手,只是在探索如何使用枚举。我有带有枚举的简单代码。

#[derive(Debug)]
enum Thing {
    Str(String),
    Boolean(bool)
}

fn run() -> Vec<Thing> {
    let mut output: Vec<Thing> = Vec::new();
    output.push(Thing::Str("Hello".to_string()));
    output.push(Thing::Boolean(true));
    return output
}

fn main() {

    let result = run();
    for i in result {
        println!("{:?}", i)
    }
}

Run Code Online (Sandbox Code Playgroud)

当我运行这段代码时,它工作正常,但我得到这样的输出

Str("Hello")
Boolean(true)
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到像

"Hello"
true
Run Code Online (Sandbox Code Playgroud)

所以我可以使用这个值进行进一步的处理,比如连接字符串或类似的东西。

感谢帮助。

enums rust

4
推荐指数
1
解决办法
58
查看次数

如何将 &amp;str 转换为 JSON 响应?

我有一个&str

{"index":0,"name":"AB/CDE/FG/402/test_int4","sts":"on","time":"2021-06-05 03:28:24.044284300 UTC","value":8}
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它转换为 JSON?

{
"index": 0,
"name": "AB/CDE/FG/402/test_int4",
"sts": "on",
"time": "2021-06-05 03:28:24.044284300 UTC",
"value": 8 
}
Run Code Online (Sandbox Code Playgroud)

有没有类似 Python 的json.loads()方法可用或者可以在 Rust 中完成类似的操作?

json rust

4
推荐指数
1
解决办法
9098
查看次数

标签 统计

rust ×2

enums ×1

json ×1