Rustc: inputting code strait from the command line instead of a file

I want to be able to do something like this:

rustc --source "fn main() { println!("Hello world"); }

Thereafter, rustc compiles and executes the code with output channeled through stdout. I want to do this over saving the data to the hard drive for efficiency reasons. Is this possible to achieve? Thanks,

Thomas B

Save it on a ram disk such as /tmp ?

1 Like

Thanks, I didn't know /tmp was stored on the RAM. That makes sense. BTW, long time no see. I hope your math degree is going well and is interesting :slight_smile: . Happy holidays!

It's not a ram disk on every distro, but it is the norm. Alternatively there's /dev/shm which is always a ram disk.

And thanks :slight_smile:

This is why I did https://crates.io/crates/runner - so one can do runner -e 'println!("{}", 42)', as well as accept snippets as files (inspired by runnable doc examples).

1 Like

Looks like a neat project Steve!

Ok wow, this is exactly what I need! I really like scripting in rust but the statically linked binary size was a really big turnoff for me. I had tried doing things dynamically but it was really cumbersome. Can this do stuff like compile dynamically in release mode as well?

cargo script is a tool to make doing this easier.

Sure, it understands -O for optimize. To run the dynamically linked exes they need to be able to see the Rust shared library (LD_LIBRARY_PATH, on Windows just drop it on the path somewhere).

There are unfortunately limits to dynamic linking once you involve other crates. Some library crates can be easily dynamically compiled, others are awkward.