cargo-script
cargo-script copied to clipboard
add support for input from terminal stdin
Is it possible to add support for input from terminal stdin like below?
cargo script <<'EOF'
// cargo-deps: time
use time;
fn main(){
println!("I'm from stdin! {}", time::now().rfc822z());
}
EOF
That way it's very straight forward to experiment in the air without any file storage.
https://neosmart.net/blog/self-compiling-rust-code/
#!/bin/sh
#![allow()] /*
# rust self-compiler by M. Al-Qudsi, licensed as public domain or MIT.
# See <https://neosmart.net/blog/self-compiling-rust-code/> for info & updates.
OUT=/tmp/$(printf "%s" $(realpath $(which "$0")) | md5sum | cut -d' ' -f1)
MD5=$(md5sum "$0" | cut -d' ' -f1)
(test "${MD5}" = "$(cat "${OUT}.md5" 2>/dev/null)" ||
(grep -Eq '^\s*(\[.*?\])*\s*fn\s*main\b*' "$0" && (rm -f ${OUT};
rustc "$0" -o ${OUT} && printf "%s" ${MD5} > ${OUT}.md5) || (rm -f ${OUT};
printf "fn main() {//%s\n}" "$(cat $0)" | rustc - -o ${OUT} &&
printf "%s" ${MD5} > ${OUT}.md5))) && exec ${OUT} $@ || exit $? #*/
// Wrapping your code in `fn main() { … }` is altogether optional :)
fn main() {
let name = std::env::args().skip(1).next().unwrap_or("world".into());
println!("Hello, {}!", name);
}
It's also easy to do!