Function evcxr::runtime_hook
source · pub fn runtime_hook()
Expand description
Binaries can call this just after staring. If we detect that we’re actually running as a subprocess, control will not return.
Examples found in repository?
examples/example_eval.rs (line 21)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() -> Result<(), Error> {
// You must call ```evcxr::runtime_hook()``` at the top of main, otherwise
// the library becomes a fork-bomb.
evcxr::runtime_hook();
let (mut context, outputs) = EvalContext::new()?;
context.eval("let mut s = String::new();")?;
context.eval(r#"s.push_str("Hello, ");"#)?;
context.eval(r#"s.push_str("World!");"#)?;
context.eval(r#"println!("{}", s);"#)?;
// For this trivial example, we just receive a single line of output from
// the code that was run. In a more complex case, we'd likely want to have
// separate threads waiting for output on both stdout and stderr.
if let Ok(line) = outputs.stdout.recv() {
println!("{line}");
}
Ok(())
}