]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/library-features/fn-traits.md
Rollup merge of #45173 - laumann:suggest-misspelled-labels, r=petrochenkov
[rust.git] / src / doc / unstable-book / src / library-features / fn-traits.md
1 # `fn_traits`
2
3 The tracking issue for this feature is [#29625]
4
5 See Also: [`unboxed_closures`](language-features/unboxed-closures.html)
6
7 [#29625]: https://github.com/rust-lang/rust/issues/29625
8
9 ----
10
11 The `fn_traits` feature allows for implementation of the [`Fn*`] traits
12 for creating custom closure-like types.
13
14 [`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html
15
16 ```rust
17 #![feature(unboxed_closures)]
18 #![feature(fn_traits)]
19
20 struct Adder {
21     a: u32
22 }
23
24 impl FnOnce<(u32, )> for Adder {
25     type Output = u32;
26     extern "rust-call" fn call_once(self, b: (u32, )) -> Self::Output {
27         self.a + b.0
28     }
29 }
30
31 fn main() {
32     let adder = Adder { a: 3 };
33     assert_eq!(adder(2), 5);
34 }
35 ```