// Copyright 2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. struct State; type Error = (); trait Bind { type Output; fn bind(self, f: F) -> Self::Output; } fn bind(mut a: A, mut f: F) -> impl FnMut(&mut State) -> Result where F: FnMut(T) -> B, A: FnMut(&mut State) -> Result, B: FnMut(&mut State) -> Result { move |state | { let r = a(state)?; f(r)(state) } } fn atom(x: T) -> impl FnMut(&mut State) -> Result { let mut x = Some(x); move |_| x.take().map_or(Err(()), Ok) } fn main() { assert_eq!(bind(atom(5), |x| atom(x > 4))(&mut State), Ok(true)); }