summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_error_codes/src/error_codes/E0525.md
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_error_codes/src/error_codes/E0525.md')
-rw-r--r--compiler/rustc_error_codes/src/error_codes/E0525.md42
1 files changed, 42 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0525.md b/compiler/rustc_error_codes/src/error_codes/E0525.md
new file mode 100644
index 000000000..a769440ca
--- /dev/null
+++ b/compiler/rustc_error_codes/src/error_codes/E0525.md
@@ -0,0 +1,42 @@
+A closure was used but didn't implement the expected trait.
+
+Erroneous code example:
+
+```compile_fail,E0525
+struct X;
+
+fn foo<T>(_: T) {}
+fn bar<T: Fn(u32)>(_: T) {}
+
+fn main() {
+ let x = X;
+ let closure = |_| foo(x); // error: expected a closure that implements
+ // the `Fn` trait, but this closure only
+ // implements `FnOnce`
+ bar(closure);
+}
+```
+
+In the example above, `closure` is an `FnOnce` closure whereas the `bar`
+function expected an `Fn` closure. In this case, it's simple to fix the issue,
+you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
+be ok:
+
+```
+#[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
+struct X;
+
+fn foo<T>(_: T) {}
+fn bar<T: Fn(u32)>(_: T) {}
+
+fn main() {
+ let x = X;
+ let closure = |_| foo(x);
+ bar(closure); // ok!
+}
+```
+
+To better understand how these work in Rust, read the [Closures][closures]
+chapter of the Book.
+
+[closures]: https://doc.rust-lang.org/book/ch13-01-closures.html