Help in understanding how closures work

I'm new to Rust, looking for some help in understanding the below note from
https://doc.rust-lang.org/stable/book/ch13-01-closures.html

Note: Functions can implement all three of the Fn traits too. 
If what we want to do doesn’t require capturing a value from the environment, 
we can use the name of a function rather than a closure where we need something 
that implements one of the Fn traits. For example, on an Option<Vec<T>> value, 
we could call unwrap_or_else(Vec::new) to get a new, empty vector if the value is None.

How can functions implement traits? As per my understanding, only structs, closures and primitive types can implement traits.

Which Fn trait does Vec::new function implement?

Thanks

All types can implement traits, and every function item (fn) or closure has a unique function item type or closure type.

Which Fn trait does Vec::new function implement?

Vec::new is a function item. Per the reference:

All function items implement Fn, FnMut, FnOnce, Copy, Clone, Send, and Sync.

3 Likes

No, what makes you think that?

All three. Free functions (that don't capture anything) don't have any environment to move or mutate, so they vacuously satisfy the requirements of Fn, FnMut, and FnOnce.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.