Skip to content
0:00 / 0:00

#rust#closures#fntraits

Crust of Rust: Functions, Closures, and Their Traits

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 5 people: 5 in engineering communities.
  • Presented by Jon Gjengset

Intermediate. Assumes working knowledge.

114K views5 years ago5 recommendations

Programming languagesRust

Jon Gjengset explains the differences between function items, function pointers, and closures in Rust, and how they implement the Fn traits.

In this episode, we go over the differences between function items, function pointers, and closures, as well as how they interact with the Fn* traits. We also touch upon dynamically dispatched Fns and experimental const Fn bounds. I've spliced out some audio issues that occurred on the live-stream, but hopefully it won't be too noticeable :p Introduction Rust for Rustaceans Function items Function pointers Function traits Closures Non-static closures dyn Fn const Fn for bounds closures in async fn Live version with chat: youtu.be/QVK4Ooo_PqM

Why it's here

5 people recommend this

What people say. Highly recommended by Rust developers as the definitive guide to understanding closures, Fn traits, and function pointers.

Moments people linked to

u/kinoshitajonain r/rust • Recommends

On “Are functions as parameters good practice?”

You need to understand the difference between fn(), Fn(), FnMut(), and FnOnce(), and managing ownership of the arguments and return types will become more difficult as you are passing around borrows and non-Copy types. (Your example uses a Copy type which makes things very simple and easy.) That said, it's not uncommon, or bad practice. Watch this video from start to finish and you'll get a hint of the difficulty that comes when you don't have a garbage collector and pass around shared mutable references to captured state left and right. (Like in JS) link to this video

u/JonhooRust for Rustaceansin r/rust • Recommends

On “Uncovered Intermediate Topics”

Are you looking for something beyond link to this video and link to this video ?

u/HellsHeroin r/rust • Recommends

On “What's the difference between `FnOnce`, `Fn` and `FnMut`”

As a supplemental resource to the excellent answers you were provided here, you can also watch Jon Gjengset's video on the topic starting from 11:24 link to this video

u/ritobanrcin r/rust • Recommends

On “Uncovered Intermediate Topics”

Jon has already covered them briefly here: link to this video and I'm not sure there's that much more to know than what he says there. Its useful when you have something in a trait bound which needs to express a relationship between its different lifetime arguments.
2View on Reddit

u/kinoshitajonain r/rust • Recommends

On “Fellow Rust enthusiasts: What "sucks" about Rust?”

Watch this video. It really helped me grok closures. link to this video

u/mrfistulain r/rust • Recommends

On “I'm a writer working at a blockchain startup. looking to learn Rust so that I can communicate better with my teammates”

Rust is a great language to read and maintain (if you can actually make your change compile it probably won't fuck anything up in a way that's hard to figure out). However learning to \*write\* it is a PITA. I think that the "crust of rust" YouTube videos (for example: link to this video though long, will give you a great flavor of the rust thought process