Rewrite tuples

pull/67/head
Dhghomon 4 years ago committed by GitHub
parent 9486909565
commit 7a52cdbc09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1970,25 +1970,35 @@ fn main() {
## Tuples
Tuples in Rust use `()`. We have seen many empty tuples already. `fn do_something() {}` has an empty tuple. Also, when you don't return anything in a function, you actually return an empty tuple.
Tuples in Rust use `()`. We have seen many empty tuples already, because *nothing* in a function actually means an empty tuple:
```rust
fn main() {
```text
fn do_something() {}
```
}
is actually short for:
```text
fn do_something() -> () {}
```
That function gets nothing (an empty tuple), and returns nothing (an empty tuple). So we have been using tuples a lot already. When you don't return anything in a function, you actually return an empty tuple.
```rust
fn just_prints() {
println!("I am printing"); // Adding ; means we return an empty tuple
}
fn main() {
}
```
But tuples can hold many things, and can hold different types too. To access the items inside of a tuple, don't use `[]`, use `.`.
But tuples can hold many things, and can hold different types too. Items inside a tuple are also indexed with numbers 0, 1, 2, and so on. But to access them, you use a `.` instead of a `[]`. Let's put a whole bunch of types into a single tuple.
```rust
fn main() {
let mut new_vec = Vec::new();
new_vec.push('a');
let random_tuple = ("Here is a name", 8, new_vec, 'b', [8, 9, 10], 7.7);
let random_tuple = ("Here is a name", 8, vec!['a'], 'b', [8, 9, 10], 7.7);
println!(
"Inside the tuple is: First item: {:?}
Second item: {:?}
@ -2006,18 +2016,54 @@ Sixth item: {:?}",
}
```
You can use a tuple to create multiple variables.
This prints:
```text
Inside the tuple is: First item: "Here is a name"
Second item: 8
Third item: ['a']
Fourth item: 'b'
Fifth item: [8, 9, 10]
Sixth item: 7.7
```
That tuple is of type `(&str, i32, Vec<char>, char, [i32; 3], f64)`.
You can use a tuple to create multiple variables. Take a look at this code:
```rust
fn main() {
let str_vec = vec!["one", "two", "three"];
}
```
`str_vec` has three items in it. What if we want to pull them out? That's where we can use a tuple.
```rust
fn main() {
let str_vec = vec!["one", "two", "three"];
let (a, b, c) = (str_vec[0], str_vec[1], str_vec[2]);
let (a, b, c) = (str_vec[0], str_vec[1], str_vec[2]); // call them a, b, and c
println!("{:?}", b);
}
```
There are many more collection types, and many more ways to use arrays, vecs, and tuples. We will learn more about them. But first we will learn control flow.
That prints `"two"`, which is what `b` is. This is called *destructuring*. That is because first the variables are inside a structure, but then we made `a`, `b`, and `c` that are not inside a structure.
If you need to destructure but don't want all the variables, you can use `_`.
```rust
fn main() {
let str_vec = vec!["one", "two", "three"];
let (_, _, variable) = (str_vec[0], str_vec[1], str_vec[2]);
}
```
Now it only creates a variable called `variable` but doesn't make a variable for the others.
There are many more collection types, and many more ways to use arrays, vecs, and tuples. We will learn more about them too, but first we will learn control flow.
## Control flow

Loading…
Cancel
Save