mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-24 04:59:41 +00:00
@ -1,29 +1,29 @@
|
|||||||
# Variables
|
# Variables
|
||||||
|
|
||||||
### Binding and mutablity
|
### Binding and mutability
|
||||||
1. 🌟 A variable can be used only if it has been initialized.
|
1. 🌟 A variable can be used only if it has been initialized.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the error below with least modifying
|
// Fix the error below with least amount of modification to the code
|
||||||
fn main() {
|
fn main() {
|
||||||
let x: i32; // uninitialized but using, ERROR !
|
let x: i32; // Uninitialized but used, ERROR !
|
||||||
let y: i32; // uninitialized but also unusing, only warning
|
let y: i32; // Uninitialized but also unused, only a Warning !
|
||||||
|
|
||||||
assert_eq!(x, 5);
|
assert_eq!(x, 5);
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 🌟 Use `mut` to mark a variable as mutable.
|
2. 🌟 Use `mut` to mark a variable as mutable.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blanks in code to make it compile
|
// Fill the blanks in the code to make it compile
|
||||||
fn main() {
|
fn main() {
|
||||||
let __ = 1;
|
let __ = 1;
|
||||||
__ += 2;
|
__ += 2;
|
||||||
|
|
||||||
assert_eq!(x, 3);
|
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. 🌟
|
3. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the error below with least modifying
|
// Fix the error below with least amount of modification
|
||||||
fn main() {
|
fn main() {
|
||||||
let x: i32 = 10;
|
let x: i32 = 10;
|
||||||
{
|
{
|
||||||
@ -47,7 +47,7 @@ fn main() {
|
|||||||
4. 🌟🌟
|
4. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the error with using of define_x
|
// Fix the error with the use of define_x
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{}, world", x);
|
println!("{}, world", x);
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ You can declare a new variable with the same name as a previous variable, here w
|
|||||||
5. 🌟🌟
|
5. 🌟🌟
|
||||||
```rust,editable
|
```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() {
|
fn main() {
|
||||||
let x: i32 = 5;
|
let x: i32 = 5;
|
||||||
{
|
{
|
||||||
@ -81,28 +81,28 @@ fn main() {
|
|||||||
6. 🌟🌟
|
6. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// remove a line in code to make it compile
|
// Remove a line in the code to make it compile
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x: i32 = 1;
|
let mut x: i32 = 1;
|
||||||
x = 7;
|
x = 7;
|
||||||
// shadowing and re-binding
|
// Shadowing and re-binding
|
||||||
let x = x;
|
let x = x;
|
||||||
x += 3;
|
x += 3;
|
||||||
|
|
||||||
|
|
||||||
let y = 4;
|
let y = 4;
|
||||||
// shadowing
|
// Shadowing
|
||||||
let y = "I can also be bound to text!";
|
let y = "I can also be bound to text!";
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Unused varibles
|
### Unused variables
|
||||||
1. fix the warning below with :
|
1. Fix the warning below with :
|
||||||
|
|
||||||
- 🌟 only one solution
|
- 🌟 Only one solution
|
||||||
- 🌟🌟 two distinct solutions
|
- 🌟🌟 Two distinct solutions
|
||||||
|
|
||||||
> Note: none of the solutions is to remove the line `let x = 1`
|
> Note: none of the solutions is to remove the line `let x = 1`
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ fn main() {
|
|||||||
let x = 1;
|
let x = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// warning: unused variable: `x`
|
// Warning: unused variable: `x`
|
||||||
```
|
```
|
||||||
|
|
||||||
### Destructuring
|
### Destructuring
|
||||||
@ -122,7 +122,7 @@ fn main() {
|
|||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the error below with least modifying
|
// Fix the error below with least amount of modification
|
||||||
fn main() {
|
fn main() {
|
||||||
let (x, y) = (1, 2);
|
let (x, y) = (1, 2);
|
||||||
x += 2;
|
x += 2;
|
||||||
@ -130,12 +130,12 @@ fn main() {
|
|||||||
assert_eq!(x, 3);
|
assert_eq!(x, 3);
|
||||||
assert_eq!(y, 2);
|
assert_eq!(y, 2);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Destructuring assignments
|
### 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. 🌟🌟
|
9. 🌟🌟
|
||||||
|
|
||||||
@ -147,10 +147,10 @@ fn main() {
|
|||||||
let (x, y);
|
let (x, y);
|
||||||
(x,..) = (3, 4);
|
(x,..) = (3, 4);
|
||||||
[.., y] = [1, 2];
|
[.., y] = [1, 2];
|
||||||
// fill the blank to make the code work
|
// Fill the blank to make the code work
|
||||||
assert_eq!([x,y], __);
|
assert_eq!([x,y], __);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user