Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for input from terminal stdin #81

Open
futurist opened this issue Sep 20, 2021 · 1 comment
Open

add support for input from terminal stdin #81

futurist opened this issue Sep 20, 2021 · 1 comment

Comments

@futurist
Copy link

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.

@dev-ardi
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants