Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing tuple elements using dot notation #80

Open
wirelessringo opened this issue Apr 24, 2019 · 0 comments
Open

Accessing tuple elements using dot notation #80

wirelessringo opened this issue Apr 24, 2019 · 0 comments

Comments

@wirelessringo
Copy link

In the "Data Types" section, a way of accessing the elements of a tuple discussed in this tutorial is through destructuring. Under "Tuple structs", it is suggested that this is the only way of accessing tuple elements:

Their fields must be accessed by destructuring (like a tuple), rather than by name.

However, there is another way of accessing tuple elements: through "dot notation". Tuple elements can be accessed directly by using a period (.) followed by the index of the value. For example:

fn bar(x: (i32, i32)) {
    println!("x was ({}, {})", x.0, x.1); // Note the `x.0` and `x.1`.
}

This can also be applied to tuple structs:

struct IntPoint (i32, i32);

fn foo(x: IntPoint) {
    println!("x was ({}, {})", x.0, x.1); // Note the `x.0` and `x.1`.
}

It is quite intriguing to me as to why this is left out, because this syntax is encountered early on in the Rust Programming Language book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant