I was a bit apprehensive because rust has like a gazillion different function types but here it seems to work like just any other language with a HM type system.
I was a bit apprehensive because rust has like a gazillion different function types but here it seems to work like just any other language with a HM type system.
The fn(T)->R syntax works for functions without associated data, it discards details of the implementation and works like function pointers in C. This allows them to be copy and 'static.
The other function types can have data with them and have more type information at compile time which allows them to be inlined.
These functions each have their own unwritable type that implements the function traits (Fn(T)->R, FnMut(T)->R and FnOnce(T)->R) depending on their enclosed data.
I hope I remembered everything right from this video by Jon Gjengset.
Something i didnt know for a long time (even though its mentioned in the book pretty sure) is that enum discriminants work like functions
#[derive(Debug, PartialEq, Eq)] enum Foo { Bar(i32), } let x: Vec<_> = [1, 2, 3] .into_iter() .map(Foo::Bar) .collect(); assert_eq!( x, vec![Foo::Bar(1), Foo::Bar(2), Foo::Bar(3)] );
Not too crazy but its something that blew my mind when i first saw it
Clippy will warn you if you don’t use this feature.
Yea it’s like when we write
Some(2)
. It’s not a function call but a variant of theOption
enum.Enum constructors are functions, this typechecks:
fn foo<T>() { let f: fn(T) -> Option<T> = Some; }
I was a bit apprehensive because rust has like a gazillion different function types but here it seems to work like just any other language with a HM type system.
Woah. That’s quite interesting. I didn’t know that.
The
fn(T)->R
syntax works for functions without associated data, it discards details of the implementation and works like function pointers in C. This allows them to be copy and 'static.The other function types can have data with them and have more type information at compile time which allows them to be inlined.
These functions each have their own unwritable type that implements the function traits (
Fn(T)->R
,FnMut(T)->R
andFnOnce(T)->R
) depending on their enclosed data.I hope I remembered everything right from this video by Jon Gjengset.