Skip to content
0:00 / 0:00

#closures#fnpointers#fnitems

Rust Functions Are Weird (But Be Glad)

Logan Smith

51K subscribers • 6 videos on Hallway

YouTube
  • Recommended by 4 people: 4 in engineering communities.
  • Presented by Logan Smith

Intermediate. Assumes working knowledge.

226K views3 years ago4 recommendations

Programming languagesSystems programmingRustC++Compilers and interpreters

Logan Smith explains how Rust handles function types and closures, comparing its zero-overhead generic codegen to C++.

Rust takes a unique approach to function types, for both closures and fn items. In this video we'll talk about a way to fit these strange function types into your existing understanding of what types are. Then we'll look at how another language (okay, it's C++) does function types in a way that causes poor codegen in generic higher-order functions if you aren't careful--and how/why Rust avoids this problem. Compiler Explorer - godbolt.org/ Godbolt code samples from the video: C++ - godbolt.org/z/xo83Ecfqb Rust - rust.godbolt.org/z/E5fvaxWPM *Rust Stuff* fn pointers vs. fn items - doc.rust-lang.org/std/primitive.fn.html#creating-function-po… Fn (the trait) - doc.rust-lang.org/std/ops/trait.Fn.html *C++ Stuff* Decay - en.cppreference.com/w/cpp/types/decay Boost.TypeIndex - boost.org/doc/libs/1_82_0/doc/html/boost_typeindex.html std::reduce - en.cppreference.com/w/cpp/algorithm/reduce Ranges - en.cppreference.com/w/cpp/ranges static - en.cppreference.com/w/cpp/language/storage_duration I use the amazing Manim library for animating these videos, and I edit them with Blender and Audacity. manim.community/ blender.org/ audacityteam.org/

Why it's here

4 people recommend this

What people say. Recommended for its clear explanation of why Rust functions have unique unnamable types and how they optimize performance.

u/Tubthumper8in r/rust • Recommends

On “Why do unnamable types exists in rust?”

One reason is for efficient compilation of higher order functions. There's a good video here that goes into greater detail and is worth watching

u/bayovakin r/rust • Recommends

On “I met someone today who's first language was rust. They are doing a degree, but it seems before this they just sat down and learned to program and chose rust because of its popularity. I am very jealous.”

Some things you said are indeed nice to haves. Some things are basically impossible in a zero-overhead language. Some things you mentioned are wrong, in my opinion. For example inheritance is an anti-pattern, and should never be part of a language. Even in use-cases where people cite inheritance as great, such as GUI and gaming, it's in fact a bad design choice. In Rust, there's a good reason why functions have different types. See link to this video The above video summarizes Rust's philosophy well, which includes performance and practicality.

u/shader301202in r/rust • Recommends

On “Is using lots of function pointers bad for performance?”

Logan Smith has made a great video on this link to this video

u/TrinitronXin r/rust • Recommends

On “What would you present to C++ developers interested in Rust?”

This video has some good examples comparing functions and type system benefits between Rust and C++ That channel also tends to have a lot of good comparisons to C++ in their other videos too.