add solutions for variables
This commit is contained in:
parent
3b805ff8a4
commit
47d37b6940
|
@ -0,0 +1,121 @@
|
|||
1.
|
||||
```rust
|
||||
fn main() {
|
||||
let x: i32 = 5; // uninitialized but using, ERROR !
|
||||
let y: i32; // uninitialized but also unusing, only warning
|
||||
println!("{} is equal to 5", x);
|
||||
}
|
||||
```
|
||||
|
||||
2.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut x = 1;
|
||||
x += 2;
|
||||
|
||||
println!("{} is equal to 3", x);
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
```rust
|
||||
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 {}", x);
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
fn main() {
|
||||
let x = define_x();
|
||||
println!("{}, world", x);
|
||||
}
|
||||
|
||||
fn define_x() -> String {
|
||||
let x = "hello".to_string();
|
||||
x
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```rust
|
||||
fn main() {
|
||||
let x: i32 = 5;
|
||||
{
|
||||
let x = 12;
|
||||
assert_eq!(x, 12);
|
||||
}
|
||||
|
||||
assert_eq!(x, 5);
|
||||
|
||||
let x = 42;
|
||||
println!("{}", x); // Prints "42".
|
||||
}
|
||||
```
|
||||
|
||||
6.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut x: i32 = 1;
|
||||
x = 7;
|
||||
// shadowing and re-binding
|
||||
let x = x;
|
||||
// x += 3;
|
||||
|
||||
|
||||
let y = 4;
|
||||
// shadowing
|
||||
let y = "I can also be bound to text!";
|
||||
}
|
||||
```
|
||||
|
||||
7.
|
||||
```rust
|
||||
fn main() {
|
||||
let _x = 1;
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
#[allow(unused_variables)]
|
||||
fn main() {
|
||||
let x = 1;
|
||||
}
|
||||
```
|
||||
|
||||
8.
|
||||
```rust
|
||||
fn main() {
|
||||
let (mut x, y) = (1, 2);
|
||||
x += 2;
|
||||
|
||||
assert_eq!(x, 3);
|
||||
assert_eq!(y, 2);
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let (x, y) = (1, 2);
|
||||
let x = 3;
|
||||
|
||||
assert_eq!(x, 3);
|
||||
assert_eq!(y, 2);
|
||||
}
|
||||
```
|
||||
|
||||
9.
|
||||
```rust
|
||||
fn main() {
|
||||
let (x, y);
|
||||
(x,..) = (3, 4);
|
||||
[.., y] = [1, 2];
|
||||
// fill the blank to make the code work
|
||||
assert_eq!([x,y], [3,2]);
|
||||
}
|
||||
```
|
|
@ -1,7 +1,7 @@
|
|||
# Variables
|
||||
|
||||
### Binding and mutablity
|
||||
π 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
|
||||
|
||||
// fix the error below with least modifying
|
||||
|
@ -12,7 +12,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
π Use `mut` to mark a variable as mutable.
|
||||
2. π Use `mut` to mark a variable as mutable.
|
||||
```rust,editable
|
||||
|
||||
// fill the blanks in code to make it compile
|
||||
|
@ -26,9 +26,10 @@ fn main() {
|
|||
|
||||
### Scope
|
||||
A scope is the range within the program for which the item is valid.
|
||||
|
||||
3. π
|
||||
```rust,editable.
|
||||
|
||||
π
|
||||
// fix the error below with least modifying
|
||||
fn main() {
|
||||
let x: i32 = 10;
|
||||
|
@ -40,10 +41,10 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
ππ
|
||||
4. ππ
|
||||
```rust,editable
|
||||
|
||||
// fix the error
|
||||
// fix the error with using of define_x
|
||||
fn main() {
|
||||
println!("{}, world", x);
|
||||
}
|
||||
|
@ -56,7 +57,7 @@ fn define_x() {
|
|||
### Shadowing
|
||||
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.
|
||||
|
||||
ππ
|
||||
5. ππ
|
||||
```rust,editable
|
||||
|
||||
// only modify `assert_eq!` to make the `println!` work(print `42` in terminal)
|
||||
|
@ -74,7 +75,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
ππ
|
||||
6. ππ
|
||||
```rust,editable
|
||||
|
||||
// remove a line in code to make it compile
|
||||
|
@ -93,7 +94,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### Unused varibles
|
||||
fix the warning below with :
|
||||
7. fix the warning below with :
|
||||
|
||||
- π one way
|
||||
- ππ two ways
|
||||
|
@ -110,7 +111,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### Destructuring
|
||||
ππ We can use a pattern with `let` to destructure a tuple to separate variables.
|
||||
8. ππ We can use a pattern with `let` to destructure a tuple to separate variables.
|
||||
|
||||
> Tips: you can use Shadowing or Mutability
|
||||
|
||||
|
@ -129,7 +130,7 @@ fn main() {
|
|||
### Destructuring assignments
|
||||
Introducing in Rust 1.59: You can now use tuple, slice, and struct patterns as the left-hand side of an assignment.
|
||||
|
||||
π
|
||||
9. ππ
|
||||
|
||||
> Note: the feature `Destructuring assignments` need 1.59 or higher Rust version
|
||||
|
||||
|
|
Loadingβ¦
Reference in New Issue