A button to run rust snippets

52 views
Skip to first unread message

Edward K. Ream

unread,
Mar 15, 2020, 1:57:10 PM3/15/20
to leo-editor
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`.

Here is an example snippet, augmented with the fstrings crate:

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

Thomas Passin

unread,
Mar 15, 2020, 5:36:08 PM3/15/20
to leo-editor


On Sunday, March 15, 2020 at 1:57:10 PM UTC-4, Edward K. Ream wrote:
Here is the @button node that I use to run the tutorial snippets in the Rust Book.

That's the kind of thing I was thinking of to get VR3 to run other languages.

Alexey Tikhonov

unread,
Mar 19, 2020, 11:10:46 AM3/19/20
to leo-editor
Thanks a lot for this example!

I started studying Rust some time ago but didn't think that it would be SO easy to work with it from Leo!

Edward K. Ream

unread,
Mar 19, 2020, 11:12:20 AM3/19/20
to leo-editor
On Thu, Mar 19, 2020 at 10:10 AM Alexey Tikhonov <tickli...@gmail.com> wrote:

Thanks a lot for this example!

You're welcome.

I started studying Rust some time ago but didn't think that it would be SO easy to work with it from Leo!

The same kind of script can be applied to almost any language.

Edward
Reply all
Reply to author
Forward
0 new messages