Created
September 23, 2019 09:22
-
-
Save rust-play/6581da07a1987bb113d6a9651f35495a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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::fmt::Display; | |
use std::ops::FnMut; | |
fn main() { | |
let a: u64 = 1.cast(); | |
let b: u16 = 1.cast(); | |
let mut v: Vec<&dyn Display> = Vec::new(); | |
v.push(&true); | |
let mut cl = gen_counter(1); | |
cl(); | |
let n = cl(); | |
println!("{}", n); | |
let k = b'9'; | |
match k { | |
b'0'...b'9' => { | |
println!("yes"); | |
} | |
_ => { | |
println!("no"); | |
} | |
} | |
} | |
fn gen_counter(init: i32) -> impl FnMut() -> i32 { | |
let mut n = init; | |
move || { | |
let ret = n; | |
n += 1; | |
ret | |
} | |
} | |
trait As<T> { | |
fn cast(self) -> T; | |
} | |
impl As<u64> for u8 { | |
fn cast(self) -> u64 { | |
self as u64 | |
} | |
} | |
impl As<u16> for u8 { | |
fn cast(self) -> u16 { | |
self as u16 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment