你好,我是 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)
所以我可以使用这个值进行进一步的处理,比如连接字符串或类似的东西。
感谢帮助。
我有一个&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 中完成类似的操作?