4

Weird title, anyway, in C function parameters are as follows:

void func_name(int a, int b) {

}

However in Rust:

fn func_name(a: int, b: int) {

}

Is this just a preference in syntax and was appealing to the creators of Rust, or is this for a specific purpose that I don't know about? For example, Go has "optional semi-colons", but they are actually to show when an expression ends. Please bare in mind that I'm a complete novice at Rust, so if you try to provide some fancy examples in Rust I probably wont understand :(

5
  • 2
    I think that syntax has more to do with the background of rust's creators. The x: type is used by ML, and ML has influenced the design of Rust (as far as I know, the original rust compiler was written in OCaml). Commented Apr 11, 2015 at 16:29
  • 2
    What's the benefit of the way C does it? Commented Apr 11, 2015 at 16:46
  • 2
    One downside of the C way is the Right-Left rule Commented Apr 11, 2015 at 17:22
  • 1
    My guess is that it's easier to parse like that. Given Rust's type inference, putting the variable type after the variable allows for it to be ommitted at times (yet still easily parsed). The parameter syntax is roughly consistent with normal variable declaration (it would be weird if Rust's function parameters were declared like they are in C, given that it would be totally inconsistent with how other Rust variables can be declared). Commented Apr 11, 2015 at 17:45
  • 3
    The var: type syntax goes back at least to Pascal. Commented Apr 11, 2015 at 18:20

2 Answers 2

7

The declaration of a function argument is just a special case of variable declarations in Rust, therefore the answer to your question lies in variable declaration in general.

Let us start with C:

a b = 1;
a = 2;

From a grammar point of view, C is not quite regular:

  • in a b = 1;, a is the type and b is the name of a new variable being declared (and initialized)
  • in a = 1;, a is the name of a variable that was declared previously and is now either initialized or assigned a new value (overwriting the previous one).

Therefore, in C, knowing whether a is a type or a variable name requires looking ahead (ie, if followed by another variable then it's a type, otherwise it's a variable).

Now, in Rust:

let a = 1;
a = 2;

The syntax for introducing a new variable requires using the let keyword, there is no ambiguity and no need to look ahead to disambiguate. This is all the more important because of shadowing in Rust (let a = ...; let a = a.foo;).

The question was about types though, so let's extend the example:

let a: b = 1;
a = 2;

In this case, again, there is no need to look ahead. Immediately after let comes the variable name, and only after parsing a : comes the variable type.

Therefore, the syntax of Rust is simply meant to avoid look ahead (Rust aims at having a LL(1) syntax) and the syntax of function arguments simply follows the regular syntax.

Oh, and by the way, not all arguments have a type:

impl Foo {
    fn doit(&self);
}
Sign up to request clarification or add additional context in comments.

Comments

0

In normal Rust code a variable is declared this way:

let x : i32 = 0;

The C style is not possible because the type is optional, so the former is equivalent to this one:

let x = 0i32;

You need the let keyword to declare the intention of declaring a name.

In a function declaration the type is mandatory, the initialization is not allowed and the let keyword makes no sense. Other than that the syntax is the same:

fn foo(x : i32)

It would be weird to have a different syntax for declaring local variables and function arguments, don't you think?

2 Comments

There are C-style languages with optional types for local variables: auto in C++ and var in C#. So it is possible.
It's not obvious to me why let clashes with omitting the type. Yes, you need let, but for other reasons, and if let type var should be easier to parse than type var, as the let already cues you in for a declaration. Mind you, I'm not saying type var has no problems (I'm fairly certain it has), I'm just not sold on what is written in this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.