For testing purposes, right now I'm doing this
$ ./rust.sh
#!/bin/sh
//bin/bash -ec '[ "$0" -nt "${0%.*}" ] && /home/user/bin/rustc/bin/rustc -L /home/user/bin/rust-std-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib "$0" -o "${0%.*}"; "${0%.*}" "$@"' "$0" "$@"; exit $?
use std::env;
// https://stackoverflow.com/a/75981598
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
println!("Hello {}!", &args[1]);
} else {
println!("Hello world!");
}
}
I don't have any Cargo.toml
.
My goal is to use Rust as a scripting language, exclusively, without compiling any executables.
That's how I learn different languages.
You may also be interested in the unstable Cargo script feature, which lets you write a single-file Rust program that Cargo can build and run. However, because it is unstable, you need to use a nightly
Rust version, and it may be changed in the future.
That's where I was referred here to from Cargo does not handle `+toolchain` directives. · Issue #14363 · rust-lang/cargo · GitHub.
I don't have the disk space for the 1 GB standard Rust toolchain on this machine, so I extracted cargo
, rustc
, and rust-std
from the Nightly archive.
(However, in general it is not good practice to try to write Rust programs with zero dependencies; you'll reinvent lots of things you don't need to.)
Right now I'm working on implementing a Native Messaging host in Rust using the standard library alone, no serde
of any other dependencies. As I've done in C, C++, Python, Bash, WASI compiled from C, WAT in a Bash script passed to wasmtime
using process substitution, JavaScript (Node.js, Deno, Bun, QuickJS, txiki.js, SpiderMonkey js
shell, TypeScript).
For V8's d8
shell and Amazon Web Services - Labs llrt
I had to create a child process to intercept and read standard input, then send standard input stream back to d8
, and llrt
, respectively.
This is my second time trying to use Rust.