Skip to content

Commit 357e724

Browse files
authored
Merge pull request #341 from tonytonyjan/patch-1
Rust examples
2 parents b75996b + c64c251 commit 357e724

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

examples/rust/README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This examples directory shows some examples written in Rust.
2+
3+
You can also test the command files by running from the command line.

examples/rust/count.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::io::{self, Write};
2+
use std::{thread, time};
3+
4+
// Simple example script that counts to 10 at ~2Hz, then stops.
5+
fn main() {
6+
for i in 1..11 {
7+
println!("{}", i);
8+
io::stdout().flush().ok().expect("Could not flush stdout");
9+
thread::sleep(time::Duration::from_millis(500));
10+
}
11+
}

examples/rust/dump-env.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Standard CGI(ish) environment variables, as defined in
2+
// http://tools.ietf.org/html/rfc3875
3+
4+
use std::env;
5+
6+
const NAMES: &'static [&'static str] = &[
7+
"AUTH_TYPE",
8+
"CONTENT_LENGTH",
9+
"CONTENT_TYPE",
10+
"GATEWAY_INTERFACE",
11+
"PATH_INFO",
12+
"PATH_TRANSLATED",
13+
"QUERY_STRING",
14+
"REMOTE_ADDR",
15+
"REMOTE_HOST",
16+
"REMOTE_IDENT",
17+
"REMOTE_PORT",
18+
"REMOTE_USER",
19+
"REQUEST_METHOD",
20+
"REQUEST_URI",
21+
"SCRIPT_NAME",
22+
"SERVER_NAME",
23+
"SERVER_PORT",
24+
"SERVER_PROTOCOL",
25+
"SERVER_SOFTWARE",
26+
"UNIQUE_ID",
27+
"HTTPS",
28+
];
29+
30+
fn main() {
31+
for key in NAMES {
32+
let value = env::var(key).unwrap_or(String::from("<unset>"));
33+
println!("{}={}", key, value);
34+
}
35+
for (key, value) in env::vars() {
36+
if key.starts_with("HTTP_") {
37+
println!("{}={}", key, value);
38+
}
39+
}
40+
}

examples/rust/greeter.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::io::{self, Write};
2+
3+
// For each line FOO received on STDIN, respond with "Hello FOO!".
4+
fn main() {
5+
loop {
6+
let mut msg = String::new();
7+
io::stdin()
8+
.read_line(&mut msg)
9+
.expect("Failed to read line");
10+
let msg = msg.trim();
11+
println!("Hello {}!", msg);
12+
io::stdout().flush().ok().expect("Could not flush stdout");
13+
}
14+
}

0 commit comments

Comments
 (0)