commit
83ded37bd8
|
@ -1,29 +1,29 @@
|
|||
# Variables
|
||||
|
||||
### Binding and mutablity
|
||||
### Binding and mutability
|
||||
1. π A variable can be used only if it has been initialized.
|
||||
```rust,editable
|
||||
|
||||
// fix the error below with least modifying
|
||||
// Fix the error below with least amount of modification to the code
|
||||
fn main() {
|
||||
let x: i32; // uninitialized but using, ERROR !
|
||||
let y: i32; // uninitialized but also unusing, only warning
|
||||
let x: i32; // Uninitialized but used, ERROR !
|
||||
let y: i32; // Uninitialized but also unused, only a Warning !
|
||||
|
||||
assert_eq!(x, 5);
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
2. π Use `mut` to mark a variable as mutable.
|
||||
```rust,editable
|
||||
|
||||
// fill the blanks in code to make it compile
|
||||
// Fill the blanks in the code to make it compile
|
||||
fn main() {
|
||||
let __ = 1;
|
||||
__ += 2;
|
||||
|
||||
assert_eq!(x, 3);
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -33,7 +33,7 @@ A scope is the range within the program for which the item is valid.
|
|||
3. π
|
||||
```rust,editable
|
||||
|
||||
// fix the error below with least modifying
|
||||
// Fix the error below with least amount of modification
|
||||
fn main() {
|
||||
let x: i32 = 10;
|
||||
{
|
||||
|
@ -47,7 +47,7 @@ fn main() {
|
|||
4. ππ
|
||||
```rust,editable
|
||||
|
||||
// fix the error with using of define_x
|
||||
// Fix the error with the use of define_x
|
||||
fn main() {
|
||||
println!("{}, world", x);
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ You can declare a new variable with the same name as a previous variable, here w
|
|||
5. ππ
|
||||
```rust,editable
|
||||
|
||||
// only modify `assert_eq!` to make the `println!` work(print `42` in terminal)
|
||||
// Only modify `assert_eq!` to make the `println!` work(print `42` in terminal)
|
||||
fn main() {
|
||||
let x: i32 = 5;
|
||||
{
|
||||
|
@ -81,28 +81,28 @@ fn main() {
|
|||
6. ππ
|
||||
```rust,editable
|
||||
|
||||
// remove a line in code to make it compile
|
||||
// Remove a line in the code to make it compile
|
||||
fn main() {
|
||||
let mut x: i32 = 1;
|
||||
x = 7;
|
||||
// shadowing and re-binding
|
||||
// Shadowing and re-binding
|
||||
let x = x;
|
||||
x += 3;
|
||||
|
||||
|
||||
let y = 4;
|
||||
// shadowing
|
||||
// Shadowing
|
||||
let y = "I can also be bound to text!";
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
### Unused varibles
|
||||
1. fix the warning below with :
|
||||
### Unused variables
|
||||
1. Fix the warning below with :
|
||||
|
||||
- π only one solution
|
||||
- ππ two distinct solutions
|
||||
- π Only one solution
|
||||
- ππ Two distinct solutions
|
||||
|
||||
> Note: none of the solutions is to remove the line `let x = 1`
|
||||
|
||||
|
@ -112,7 +112,7 @@ fn main() {
|
|||
let x = 1;
|
||||
}
|
||||
|
||||
// warning: unused variable: `x`
|
||||
// Warning: unused variable: `x`
|
||||
```
|
||||
|
||||
### Destructuring
|
||||
|
@ -122,7 +122,7 @@ fn main() {
|
|||
|
||||
```rust,editable
|
||||
|
||||
// fix the error below with least modifying
|
||||
// Fix the error below with least amount of modification
|
||||
fn main() {
|
||||
let (x, y) = (1, 2);
|
||||
x += 2;
|
||||
|
@ -130,12 +130,12 @@ fn main() {
|
|||
assert_eq!(x, 3);
|
||||
assert_eq!(y, 2);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
### Destructuring assignments
|
||||
Introducing in Rust 1.59: You can now use tuple, slice, and struct patterns as the left-hand side of an assignment.
|
||||
Introduced in Rust 1.59: You can now use tuple, slice, and struct patterns as the left-hand side of an assignment.
|
||||
|
||||
9. ππ
|
||||
|
||||
|
@ -147,10 +147,10 @@ fn main() {
|
|||
let (x, y);
|
||||
(x,..) = (3, 4);
|
||||
[.., y] = [1, 2];
|
||||
// fill the blank to make the code work
|
||||
// Fill the blank to make the code work
|
||||
assert_eq!([x,y], __);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loadingβ¦
Reference in New Issue