Skip to content

In the Guide, the example of extracting a value from a newtype is confusing #20471

Closed
@camjackson

Description

@camjackson

In this section of the guide, this example is given:

struct Inches(int);

let length = Inches(10);

let Inches(integer_length) = length;
println!("length is {} inches", integer_length);

As a newcomer to Rust, I found this example quite confusing, specifically the line declaring the integer_length binding. Until this point in the Guide, all bindings are declared like let var_name = "some_value"; or let var_name: VarType;. All of a sudden, we have let SomeType(var_name) = instance_of_SomeType;, and the only explanation is "you can extract the inner integer type through a destructuring let".

It's clear enough from the context what the line is doing (extracting the inner value), but I don't really get how the code works, or why the syntax looks that way, rather than let integer_length = some_expression_involving_length;.

Also, and perhaps this should be a separate issue, I personally feel like mentioning tuple structs is unnecessary at this point in the guide? They are brought up, and then immediately dismissed in favour of proper structs, which leaves the reader (or me, at least) wondering why they exist in the language at all... Perhaps tuple structs are only explained as a precursor to showing off newtypes (which do seem useful), but I still feel like newtypes could be explained well enough without going into tuple structs, or perhaps with just a passing mention that a newtype is a special case of a thing called a tuple struct, which will be covered later.

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