NoNameProvided
NoNameProvided

Reputation: 8987

How to assign function return value to a variable in Rust?

I just started to play around with Rust. Trying to run the code snippet below will result in a compile time error with the message: type ascription is experimental.

use std::env;

fn main() {
    let arguments: Args = env:args();
}

The docs for env:args shows that the function returns an Args struct and the Variable Binding section shows that I can set type of the variables with let varname: type = value. How I can properly assign a return value of a function to a variable?

Upvotes: 1

Views: 2732

Answers (1)

Veedrac
Veedrac

Reputation: 60137

You're looking for

let arguments: Args = env::args();

Using a single colon in an expression is type ascription, and currently you can only specify types on variable bindings. Note that the : Args annotation on this line is optional.

Using two colons allows you to access items within a namespace, so one wants env::args, not env:args.

Upvotes: 6

Related Questions