Created
September 23, 2019 02:04
-
-
Save rust-play/345c2e9ae07ca55156d826bcdb3c0794 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); | |
} | |
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