]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0525.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0525.md
CommitLineData
60c5eb7d
XL
1A closure was used but didn't implement the expected trait.
2
3Erroneous code example:
4
5```compile_fail,E0525
6struct X;
7
8fn foo<T>(_: T) {}
9fn bar<T: Fn(u32)>(_: T) {}
10
11fn main() {
12 let x = X;
13 let closure = |_| foo(x); // error: expected a closure that implements
14 // the `Fn` trait, but this closure only
15 // implements `FnOnce`
16 bar(closure);
17}
18```
19
20In the example above, `closure` is an `FnOnce` closure whereas the `bar`
21function expected an `Fn` closure. In this case, it's simple to fix the issue,
22you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
23be ok:
24
25```
26#[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
27struct X;
28
29fn foo<T>(_: T) {}
30fn bar<T: Fn(u32)>(_: T) {}
31
32fn main() {
33 let x = X;
34 let closure = |_| foo(x);
35 bar(closure); // ok!
36}
37```
38
74b04a01
XL
39To better understand how these work in Rust, read the [Closures][closures]
40chapter of the Book.
41
42[closures]: https://doc.rust-lang.org/book/ch13-01-closures.html