1

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I understand the purpose of using :: to access functions in a namespace. But I don't exactly understand the reason of using :. I googled type ascription and I roughly understand what it means, but could you provide some example here to show what would be the purpose of using one semicolon in Rust?
@NoNameProvided Sometime a type can't be inferred. Type ascription makes it easier to specify. Here's an example.
@Veedrac what's the difference between that and default::Default::<String>()?
@NateMara Using ::<String> specifies the template parameters of Default::default, whereas : Default specifies the return type. In this trivial case those happen to be one and the same, but that's not always the case. Type ascription also looks nicer, IMO.

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.