Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 23, 2019 02:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/345c2e9ae07ca55156d826bcdb3c0794 to your computer and use it in GitHub Desktop.
Save rust-play/345c2e9ae07ca55156d826bcdb3c0794 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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