Skip to content

TRPL: (assignment == empty tuple), please elaborate #26120

Closed
@mcast

Description

@mcast

Hi,

I really like TRPL and have found it answers most of my questions in the footnotes.
Looking at https://github.com/rust-lang/rust/blame/stable/src/doc/trpl/functions.md#L144-L153

Note that assigning to an already-bound variable (e.g. y = 5) is still an expression,
although its value is not particularly useful. Unlike other languages where an assignment
evaluates to the assigned value (e.g. 5 in the previous example), in Rust the value of an
assignment is an empty tuple ():

let mut y = 5;
 
let x = (y = 6);  // x has the value `()`, not `6`

I guessed this difference would be to discourage "clever" coding..? It would be nice to have an explanatory link to read.

Thanks,

Matthew


ps. research done... I could find only https://internals.rust-lang.org/t/allow-loops-to-return-values-other-than/567 on loop return values.

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