mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 20:49:41 +00:00
Made the comments clearer
This commit is contained in:
@ -4,10 +4,10 @@
|
|||||||
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, will result in an ERROR !
|
||||||
let y: i32; // uninitialized but also unusing, only warning
|
let y: i32; // Uninitialized but also unused, will only receive a warning
|
||||||
|
|
||||||
assert_eq!(x, 5);
|
assert_eq!(x, 5);
|
||||||
println!("Success!")
|
println!("Success!")
|
||||||
@ -17,7 +17,7 @@ fn main() {
|
|||||||
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;
|
||||||
@ -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,17 +81,17 @@ 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!")
|
||||||
@ -99,10 +99,10 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Unused variables
|
### 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;
|
||||||
@ -147,7 +147,7 @@ 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