-
-
Save therustmonk/a4b315ba62a4b17e39c49e9cf55722ab 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
#![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