Here is the @button node that I use to run the tutorial snippets in the
Rust Book.
g.cls()
c.save()
import os
h = 'Rust Code'
p = g.findNodeAnywhere(c, h)
assert p, h
dir_ = r'c:\RustProj\run_rust\src' # Change as needed.
file_name = 'main.rs'
os.chdir(dir_)
contents = p.b.replace('@language rust', '').rstrip() + '\n'
with open(file_name, 'w') as f:
f.write(contents)
commands = [
'cargo run',
]
g.execute_shell_commands(commands)
This script does the following:
- Finds the node called 'Rust Code'.
- Writes its body text (sans `@language rust` directive) to C:\RustProj\run_rust\src\main.rs.
- Calls `cargo run` from with the proper directory.
I created the RustProj\run_rust folder using `cargo new run_rust`.
#![allow(unused_variables)]
#[macro_use]
extern crate fstrings;
fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
print_f!("calculate_length.{s1} is {len}.\n");
}
fn calculate_length(s: &String) -> usize {
s.len()
}
@language rust
This shows how easy it is to build language-specific support into Leo.
Edward