Created
March 2, 2016 23:22
-
-
Save killercup/4c1e5a37f6add896046d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
struct Person<'a> { | |
first_name: &'a str, | |
last_name: &'a str, | |
} | |
fn foo1<'bar_lifetime>(bar: &'bar_lifetime str) -> &'bar_lifetime str { | |
let a = "aa"; | |
a | |
} | |
fn foo2<'bar, 'baz>(bar: &'bar str, baz: &'baz str) -> &'bar str { | |
bar | |
} | |
fn main() { | |
println!("{:?}", foo1("hi")); | |
println!("{:?}", foo2("hi", "ho")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Fizz Buzz | |
#[test] | |
fn fizz_buzz_test() { | |
assert_eq!("buzz", check_fizz_buzz(5)); | |
} | |
#[test] | |
fn fizz_check() { | |
assert_eq!("fizzbuzz", check_fizz_buzz(15)); | |
} | |
#[test] | |
fn one_to_thirtysix() { | |
let one_to_thirtysix = "1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz, fizz, 22, 23, fizz, buzz, 26, fizz, 28, 29, fizzbuzz, 31, 32, fizz, 34, buzz, fizz"; | |
assert_eq!(fizz_buzz(36), one_to_thirtysix); | |
} | |
fn fizz_buzz(max: u32) -> String { | |
(1..(max + 1)) | |
.map(check_fizz_buzz) | |
.collect::<Vec<String>>() | |
.join(", ") | |
} | |
#[derive(Debug)] | |
enum FizzBuzz { | |
Fizz, | |
Buzz, | |
FizzBuzz, | |
Number(u32), | |
} | |
#[derive(Debug)] | |
struct Foo { | |
bar1: String, | |
bar2: String, | |
bar3: String, | |
bar4: String, | |
} | |
/// Do fizz buzz | |
fn check_fizz_buzz(number: u32) -> String { | |
match (number % 3 == 0, number % 5 == 0) { | |
(false, false) => number.to_string(), | |
(true, false) => "fizz".to_string(), | |
(false, true) => "buzz".to_string(), | |
(true, true) => "fizzbuzz".to_string() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't forget to add hyper to your Cargo.toml! | |
extern crate hyper; | |
use std::io::Read; | |
// use hyper::Client; | |
use hyper::header::Connection; | |
fn main() { | |
// Create a client. | |
let client = hyper::Client::new(); | |
// Creating an outgoing request. | |
let mut res = client.get("http://rust-lang.org/") | |
// set a header | |
.header(Connection::close()) | |
// let 'er go! | |
.send().unwrap(); | |
// Read the Response. | |
let mut body = String::new(); | |
read_something(&mut res, &mut body).unwrap(); | |
println!("Response: {}", body); | |
} | |
fn read_something<T>(what: &mut T, buffer: &mut String) -> std::io::Result<usize> | |
where T: Read, | |
{ | |
what.read_to_string(buffer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment