#setting-up #cargo #file #cargo-toml #input #serde-json

已删除 playground

在不设置 Cargo 的情况下运行 Rust 代码

0.1.0 2020年2月19日

#46 in #setting-up

MIT 协议

20KB
476

playground

playground 是一个工具,可以帮助您在不手动设置 Cargo 项目的环境下运行 Rust 代码文件。

安装

cargo install playground

用法

只需运行 playground <files> 即可。您可以在文件开头使用前缀 // 指定外部依赖。它接受与 Cargo.toml 相同的 TOML 语法。

示例

$ cat serde_json.rs
//# serde_json = "*"

use serde_json::{Result, Value};

fn main() -> Result<()> {
    // Some JSON input data as a &str. Maybe this comes from the user.
    let data = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;

    // Parse the string of data into serde_json::Value.
    let v: Value = serde_json::from_str(data)?;

    // Access parts of the data by indexing with square brackets.
    println!("Please call {} at the number {}", v["name"], v["phones"][0]);

    Ok(())
}

$ playground serde_json.rs

Please call "John Doe" at the number "+44 1234567"

依赖项

~2.1–3MB
~49K SLoC