Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rust parser need the fn keyword?

Tags:

rust

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.

like image 624
i30817 Avatar asked Apr 13 '13 16:04

i30817


People also ask

What is FN in Rust?

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.

What language uses FN?

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.


1 Answers

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 ints

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.

like image 192
huon Avatar answered Nov 17 '22 01:11

huon