Skip to content

guide: random secret number remarks #16354

Closed
@vks

Description

@vks

It might be worth mentioning that generating a uint directly is more efficient than generating an int and calling abs (unless the compiler is very clever).

When reading the code I was wondering why

let secret_number = (rand::random() % 100u) + 1;

would not compile. Shouldn't the compiler be able to infer the correct specialization of random?

Also, it might be more idiomatic to write

let secret_number = (rand::random::<uint>() % 100) + 1;

than using redundant integer literals

let secret_number = (rand::random::<uint>() % 100u) + 1u;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions