I've been reading the blogs about rust and this closure for example made me wonder:
fn each<E>(t: &Tree<E>, f: &fn(&E) -> bool) {
if !f(&t.elem) {
return;
}
for t.children.each |child| { each(child, f); }
}
why couldn't it be:
each<E>(t: &Tree<E>, f: &(&E) -> bool) {
if !f(&t.elem) {
return;
}
for t.children.each |child| { each(child, f); }
}
Maybe i'm missing something on the class system that would prevent this.
Keyword fnFunctions are the primary way code is executed within Rust. Function blocks, usually just called functions, can be defined in a variety of different places and be assigned many different attributes and modifiers.
Functions are prevalent in Rust code. You've already seen one of the most important functions in the language: the main function, which is the entry point of many programs. You've also seen the fn keyword, which allows you to declare new functions.
It makes parsing more complicated for compilers, syntax-highlighters, shell scripts and humans (i.e. everyone).
For example, with fn
, foo
takes a function that has two int
arguments and returns nothing, and bar
takes a pointer to a tuple of 2 int
s
fn foo(f: &fn(int, int)) {}
fn bar(t: &(int, int)) {}
Without fn
, the arguments for both become &(int, int)
and the compiler couldn't distinguish them. Sure one could come up with other rules so they are written differently, but these almost certainly have no advantages over just using fn
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With