diff --git a/examples/rust/README.txt b/examples/rust/README.txt new file mode 100644 index 00000000..37c97157 --- /dev/null +++ b/examples/rust/README.txt @@ -0,0 +1,3 @@ +This examples directory shows some examples written in Rust. + +You can also test the command files by running from the command line. \ No newline at end of file diff --git a/examples/rust/count.rs b/examples/rust/count.rs new file mode 100644 index 00000000..45a75009 --- /dev/null +++ b/examples/rust/count.rs @@ -0,0 +1,11 @@ +use std::io::{self, Write}; +use std::{thread, time}; + +// Simple example script that counts to 10 at ~2Hz, then stops. +fn main() { + for i in 1..11 { + println!("{}", i); + io::stdout().flush().ok().expect("Could not flush stdout"); + thread::sleep(time::Duration::from_millis(500)); + } +} diff --git a/examples/rust/dump-env.rs b/examples/rust/dump-env.rs new file mode 100644 index 00000000..092f2ca8 --- /dev/null +++ b/examples/rust/dump-env.rs @@ -0,0 +1,40 @@ +// Standard CGI(ish) environment variables, as defined in +// http://tools.ietf.org/html/rfc3875 + +use std::env; + +const NAMES: &'static [&'static str] = &[ + "AUTH_TYPE", + "CONTENT_LENGTH", + "CONTENT_TYPE", + "GATEWAY_INTERFACE", + "PATH_INFO", + "PATH_TRANSLATED", + "QUERY_STRING", + "REMOTE_ADDR", + "REMOTE_HOST", + "REMOTE_IDENT", + "REMOTE_PORT", + "REMOTE_USER", + "REQUEST_METHOD", + "REQUEST_URI", + "SCRIPT_NAME", + "SERVER_NAME", + "SERVER_PORT", + "SERVER_PROTOCOL", + "SERVER_SOFTWARE", + "UNIQUE_ID", + "HTTPS", +]; + +fn main() { + for key in NAMES { + let value = env::var(key).unwrap_or(String::from("")); + println!("{}={}", key, value); + } + for (key, value) in env::vars() { + if key.starts_with("HTTP_") { + println!("{}={}", key, value); + } + } +} diff --git a/examples/rust/greeter.rs b/examples/rust/greeter.rs new file mode 100644 index 00000000..8a458a02 --- /dev/null +++ b/examples/rust/greeter.rs @@ -0,0 +1,14 @@ +use std::io::{self, Write}; + +// For each line FOO received on STDIN, respond with "Hello FOO!". +fn main() { + loop { + let mut msg = String::new(); + io::stdin() + .read_line(&mut msg) + .expect("Failed to read line"); + let msg = msg.trim(); + println!("Hello {}!", msg); + io::stdout().flush().ok().expect("Could not flush stdout"); + } +}