#inline #rustc #embed #cargo #macro

macro inline-rust

一个愚蠢的宏,它可以编译和执行Rust代码,并将输出直接输出到您的Rust代码中

1个稳定版本

1.0.0 2021年11月22日

#1298 in 进程宏

MIT 许可协议

11KB
242

crates.io docs.rs license

inline-rust

这是一个受 inline-python 启发的愚蠢宏,它编译并执行Rust代码,并将输出直接输出到您的Rust代码中。

它有一个用例是用于评估高级的"const"表达式,见下面的示例...如果您敢的话。

用法

[dependencies]
inline-rust = "*"

示例

// Compiles using cargo
const CONST_HASH: &'static str = inline_rust!(
    r#"
        [dependencies]
        sha2 = "0.9.8"
    "#,
    {
        use sha2::Digest;

        let mut sum: i32 = 0;
        for n in 0..30 {
            sum += n;
        }

        format!("\"{:x}\"", sha2::Sha256::digest(&sum.to_ne_bytes()))
    }
);

// Compiles using rustc
const CONST_FOR_LOOP: i32 = inline_rust!({
    let mut sum: i32 = 0;
    for n in 0..30 {
        sum += n;
    }
    format!("{}", sum)
});

依赖项

~2MB
~46K SLoC