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
use std::collections::HashMap; | |
fn main() { | |
let mut router = BasicRouter::new(); | |
router.add_route("/", |req| Response { | |
code: 200, | |
headers: req.headers.clone(), | |
body: vec![1, 2, 3, 4, 5], | |
}); |
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
/* Counter example can be seen at https://doc.rust-lang.org/std/iter/index.html#implementing-iterator | |
I just wanna try out implementing Iterator trait on tuple like struct. | |
Due to the IntoIterator blanket implementation in standard library - "impl<I: Iterator> IntoIterator for I" , | |
all Iterator can be treated like IntoIterator. That's why we can call c.into_iter() in the code. | |
*/ | |
use std::iter::*; | |
fn main() { | |
let mut z = 0u32; | |
let ref mut c = Counter(&mut z); |
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
#![feature(core_intrinsics)] | |
fn print_type_of<T>(_: &T) { | |
println!("{}", unsafe { std::intrinsics::type_name::<T>() }); | |
} |
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
use std::error::Error; | |
use std::fs::File; | |
use std::io; | |
use std::io::prelude::*; | |
use std::io::{stderr, Write}; | |
fn main() { | |
let r = read_file("wrong.test"); // read_file(r"/Users/nyinyithan/Downloads/test.json"); | |
match r { | |
Ok(txt) => println!("{}", txt), |
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
let mut v = vec![1, 2, 3, 4, 5]; | |
let r = v | |
.get_mut(1..=2) | |
.map(|x| x.iter_mut().for_each(|y| *y *= 10)); | |
if r.is_some() { | |
println!("Succeed : {:?}", v); | |
} else { | |
println!("Not Succeed. Do something here."); | |
} |
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
use std::borrow::BorrowMut; | |
pub trait OptionExt<T> { | |
fn fold<S, F>(&self, state: S, folder: F) -> S | |
where | |
F: FnOnce(S, &T) -> S; | |
} | |
impl<T> OptionExt<T> for Option<T> { | |
fn fold<S, F>(&self, state: S, folder: F) -> S |
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
use std::io; | |
use std::io::prelude::*; | |
fn main() { | |
loop { | |
let r = get_input("Enter the first number: ") | |
.and_then(|x| get_input("Enter the second number: ") | |
.and_then(|y| get_input("Enter the third number: ") | |
.and_then(|z| Ok(x + y + z)))); |
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
#![feature(start)] | |
#![no_std] | |
extern crate libc; | |
use core::panic::PanicInfo; | |
use libc::{c_char, c_int, c_void, size_t}; | |
extern "C" { | |
fn malloc(size: size_t) -> *mut c_void; | |
fn free(p: *mut c_void); |
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
trait Animal { | |
fn speak(&self); | |
} | |
#[derive(Debug)] | |
struct Dog { | |
name: String, | |
age: u8, | |
} |
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
#![feature(result_map_or_else)] | |
extern crate num_cpus; | |
extern crate reqwest; | |
extern crate threadpool; | |
use std::error::Error; | |
use std::thread; | |
use threadpool::ThreadPool; |
OlderNewer