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. There are two kinds of subprocesses that we may be acting as (1) the process that loads and runs the user code and (2) a wrapper around rustc.
Examples found in repository?
examples/example_eval.rs (line 14)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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(())
}