rust-by-practice/en/src/variables.md

159 lines
3.0 KiB
Markdown
Raw Normal View History

# Variables
2022-02-24 01:50:01 -06:00
### Binding and mutability
2022-03-01 07:56:43 -06:00
1. 🌟 A variable can be used only if it has been initialized.
2022-02-24 01:50:01 -06:00
```rust,editable
2022-04-02 11:49:14 -06:00
// Fix the error below with least amount of modification to the code
2022-02-24 01:50:01 -06:00
fn main() {
2022-04-03 11:22:32 -05:00
let x: i32; // Uninitialized but used, ERROR !
2022-04-03 21:40:01 -05:00
let y: i32; // Uninitialized but also unused, only a Warning !
assert_eq!(x, 5);
2022-04-02 11:59:51 -06:00
println!("Success!");
2022-02-24 01:50:01 -06:00
}
```
2022-03-01 07:56:43 -06:00
2. 🌟 Use `mut` to mark a variable as mutable.
2022-02-24 01:50:01 -06:00
```rust,editable
2022-04-02 11:49:14 -06:00
// Fill the blanks in the code to make it compile
2022-02-24 01:50:01 -06:00
fn main() {
let __ __ = 1;
2022-02-24 01:50:01 -06:00
__ += 2;
assert_eq!(x, 3);
2022-04-02 11:59:51 -06:00
println!("Success!");
2022-02-24 01:50:01 -06:00
}
```
### Scope
2022-02-28 02:57:04 -06:00
A scope is the range within the program for which the item is valid.
2022-03-01 07:56:43 -06:00
3. 🌟
2022-03-04 06:52:30 -06:00
```rust,editable
2022-02-24 01:50:01 -06:00
2022-04-02 11:49:14 -06:00
// Fix the error below with least amount of modification
2022-02-24 01:50:01 -06:00
fn main() {
let x: i32 = 10;
{
let y: i32 = 5;
println!("The value of x is {} and value of y is {}", x, y);
}
println!("The value of x is {} and value of y is {}", x, y);
}
```
2022-03-01 07:56:43 -06:00
4. 🌟🌟
2022-02-24 01:50:01 -06:00
```rust,editable
2022-04-02 11:49:14 -06:00
// Fix the error with the use of define_x
2022-02-24 01:50:01 -06:00
fn main() {
println!("{}, world", x);
}
fn define_x() {
let x = "hello";
}
```
### Shadowing
2022-02-28 02:57:04 -06:00
You can declare a new variable with the same name as a previous variable, here we can say **the first one is shadowed by the second one.
2022-02-24 01:50:01 -06:00
2022-03-01 07:56:43 -06:00
5. 🌟🌟
2022-02-24 01:50:01 -06:00
```rust,editable
2022-04-02 11:49:14 -06:00
// Only modify `assert_eq!` to make the `println!` work(print `42` in terminal)
2022-02-24 01:50:01 -06:00
fn main() {
let x: i32 = 5;
{
let x = 12;
assert_eq!(x, 5);
}
assert_eq!(x, 12);
2022-07-04 06:51:02 -05:00
let x = 42;
2022-02-24 01:50:01 -06:00
println!("{}", x); // Prints "42".
}
```
2022-03-01 07:56:43 -06:00
6. 🌟🌟
2022-02-24 01:50:01 -06:00
```rust,editable
2022-04-02 11:49:14 -06:00
// Remove a line in the code to make it compile
2022-02-24 01:50:01 -06:00
fn main() {
let mut x: i32 = 1;
x = 7;
2022-04-02 11:49:14 -06:00
// Shadowing and re-binding
2022-02-24 01:50:01 -06:00
let x = x;
x += 3;
let y = 4;
2022-04-02 11:49:14 -06:00
// Shadowing
2022-02-24 01:50:01 -06:00
let y = "I can also be bound to text!";
2022-04-02 11:59:51 -06:00
println!("Success!");
2022-02-24 01:50:01 -06:00
}
```
### Unused variables
7. Fix the warning below with :
2022-02-24 01:50:01 -06:00
2022-04-02 11:49:14 -06:00
- 🌟 Only one solution
- 🌟🌟 Two distinct solutions
2022-02-24 01:50:01 -06:00
2022-03-02 02:37:02 -06:00
> Note: none of the solutions is to remove the line `let x = 1`
2022-02-24 01:50:01 -06:00
```rust,editable
fn main() {
let x = 1;
}
2022-04-02 11:49:14 -06:00
// Warning: unused variable: `x`
2022-02-24 01:50:01 -06:00
```
2022-02-28 02:57:04 -06:00
### Destructuring
2022-03-01 07:56:43 -06:00
8. 🌟🌟 We can use a pattern with `let` to destructure a tuple to separate variables.
2022-02-24 01:50:01 -06:00
> Tips: you can use Shadowing or Mutability
```rust,editable
2022-04-02 11:49:14 -06:00
// Fix the error below with least amount of modification
2022-02-24 01:50:01 -06:00
fn main() {
let (x, y) = (1, 2);
x += 2;
assert_eq!(x, 3);
assert_eq!(y, 2);
2022-04-02 11:59:51 -06:00
println!("Success!");
2022-02-24 01:50:01 -06:00
}
2022-02-24 21:18:27 -06:00
```
### Destructuring assignments
Introduced in Rust 1.59: You can now use tuple, slice, and struct patterns as the left-hand side of an assignment.
2022-02-24 21:18:27 -06:00
2022-03-01 07:56:43 -06:00
9. 🌟🌟
2022-02-24 21:18:27 -06:00
> Note: the feature `Destructuring assignments` need 1.59 or higher Rust version
```rust,editable
fn main() {
2022-02-28 02:57:04 -06:00
let (x, y);
(x,..) = (3, 4);
[.., y] = [1, 2];
2022-04-02 11:49:14 -06:00
// Fill the blank to make the code work
2022-02-28 02:57:04 -06:00
assert_eq!([x,y], __);
2022-04-02 11:59:51 -06:00
println!("Success!");
2022-02-24 21:18:27 -06:00
}
2022-03-01 08:06:38 -06:00
```
2022-03-04 06:52:30 -06:00
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it