Here is the @button node that I use to run the tutorial snippets in the Rust 
Book <https://doc.rust-lang.org/book/title-page.html>.

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`.

Here is an example snippet, augmented with the fstrings crate 
<https://docs.rs/fstrings/0.2.2/fstrings/>:

#![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

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/743a2273-2d6e-46fd-8380-a1685a425f12%40googlegroups.com.

Reply via email to