Skip to content

Instantly share code, notes, and snippets.

@irh
irh / variadic_closures.rs
Created September 29, 2022 08:44
An example of implementing a method that accepts closures with different arguments.
// An example of implementing a method that accepts closures with different input arguments.
// The trick to avoid Rust complaining about conflicting implementations is to specify
// the arguments for each specialization in a separate type parameter.
// See https://geo-ant.github.io/blog/2021/rust-traits-and-variadic-functions/
fn main() {
let mut foo = Foo::default();
foo.add_fn(|| 0);
foo.add_fn(|a| a);
@irh
irh / concepts_polymorphism.rs
Last active August 28, 2016 11:16
A Rust version of Sean Parent's example of concepts-based polymorphism in C++
// ...from 'Inheritance Is The Base Class of Evil'
use std::fmt;
use std::rc::Rc;
fn white_space(count: usize) -> String {
std::iter::repeat(' ').take(count).collect::<String>()
}
trait Drawable {