cargo-script icon indicating copy to clipboard operation
cargo-script copied to clipboard

add support for input from terminal stdin

Open futurist opened this issue 2 years ago • 1 comments

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.

futurist avatar Sep 20 '21 02:09 futurist

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!

dev-ardi avatar Nov 29 '23 17:11 dev-ardi