user16564170
user16564170

Reputation:

Compiling rust code from a string and run it

Is there a way to compile rust code from a string? Something like

use rustc::compile;
let code: &str = r#"fn main() { println!("Hello from Rust!"); }"#;
let result = rustc::compile(code).unwrap();
println!("{}", result);

Upvotes: 3

Views: 700

Answers (1)

Acorn
Acorn

Reputation: 26066

You need to invoke the Rust compiler. Either with a new process or linking with the compiler libraries.

But compiled programs should not compile code unless they have a very good reason.

Upvotes: 1

Related Questions