Skip to content

A cursed macro that compiles and executes Rust and spits the output directly into your Rust code

License

Notifications You must be signed in to change notification settings

WilliamVenner/inline-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crates.io docs.rs license

inline-rust

This is a stupid macro inspired by inline-python that compiles and executes Rust and spits the output directly into your Rust code.

There is a use case of using it to evaluate advanced "const" expressions, see the example below... if you dare.

Usage

[dependencies]
inline-rust = "*"

Example

// 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)
});

About

A cursed macro that compiles and executes Rust and spits the output directly into your Rust code

Topics

Resources

License

Stars

Watchers

Forks

Languages