Skip to content

Commit

Permalink
Merge pull request joewalnes#341 from tonytonyjan/patch-1
Browse files Browse the repository at this point in the history
Rust examples
  • Loading branch information
asergeyev committed Jan 29, 2019
2 parents b75996b + c64c251 commit 357e724
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/rust/README.txt
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions examples/rust/count.rs
Original file line number Diff line number Diff line change
@@ -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));
}
}
40 changes: 40 additions & 0 deletions examples/rust/dump-env.rs
Original file line number Diff line number Diff line change
@@ -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("<unset>"));
println!("{}={}", key, value);
}
for (key, value) in env::vars() {
if key.starts_with("HTTP_") {
println!("{}={}", key, value);
}
}
}
14 changes: 14 additions & 0 deletions examples/rust/greeter.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 357e724

Please sign in to comment.