Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created March 25, 2019 06:59
Show Gist options
  • Save rust-play/680b1e811f5e5685fd6a14fb008908ba to your computer and use it in GitHub Desktop.
Save rust-play/680b1e811f5e5685fd6a14fb008908ba to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(unboxed_closures)]
#![feature(fn_traits)]
use std::collections::BTreeMap;
struct IncIdGen(i32);
impl FnOnce<()> for &mut IncIdGen {
type Output = i32;
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
self.0 += 1;
self.0
}
}
fn main() {
let mut id_gen = IncIdGen(0);
let mut map = BTreeMap::new();
map.insert("c".into(), 3i32);
for id in "abcdefghijklmnopqrs".chars() {
let id = format!("{}", id);
map.entry(id).or_insert_with(&mut id_gen);
}
dbg!(map);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment