[Introduction to Control Flow in Rust] Introduction to Control Flow in Rust


Introduction to Control Flow in Rust

Control flow is a fundamental concept in programming. It determines the order in which the code is executed. In Rust, like in many other programming languages, we have conditional statements and loops to manage the control flow. Let’s dive into the first chapter and explore conditional statements in Rust.

1. Conditional Statements

1.1 ifStatement (if statement)

The if statement allows you to execute a block of code only if a certain condition is true. Here’s a basic example:

let number = 5;

if number > 3 {
    
    
    println!("The number is greater than 3."); // The number is greater than 3. (数字大于3)
}

In the above code, since the number 5 is indeed greater than 3, the message inside the println! macro will be printed.

1.2 else ifStatement (else if statement)

The else if statement comes after the if statement and checks for another condition if the previous if condition is false.

let number = 2;

if number > 3 {
    
    
    println!("The number is greater than 3.");
} else if number < 3 {
    
    
    println!("The number is less than 3."); // The number is less than 3. (数字小于3)
}

In this example, since the number 2 is not greater than 3, the code inside the else if block will be executed.

1.3 elseStatement (else statement)

The else statement is used when you want to execute a block of code if none of the previous conditions are true.

let number = 3;

if number > 3 {
    
    
    println!("The number is greater than 3.");
} else if number < 3 {
    
    
    println!("The number is less than 3.");
} else {
    
    
    println!("The number is 3."); // The number is 3. (数字是3)
}

Here, since the number is exactly 3, the code inside the else block will be executed.

In conclusion, conditional statements in Rust allow you to make decisions in your code based on certain conditions. They are essential for creating dynamic and responsive programs. In the next chapter, we will explore loops in Rust.

2. Loops

Loops are used to execute a block of code repeatedly. Rust provides several ways to loop, each with its own use case. Let’s explore the different types of loops in Rust.

2.1 loop(loop loop)

The loop keyword creates an infinite loop, which means the code inside the loop will run indefinitely until explicitly told to stop. This can be useful when you want a piece of code to run until a certain condition is met.

let mut counter = 0;

loop {
    
    
    counter += 1;
    println!("This is loop iteration number {}.", counter); // 这是循环的第{}次迭代

    if counter == 5 {
    
    
        break; // Exit the loop (退出循环)
    }
}

In the above code, the loop will run five times, and then the break statement will stop the loop.

2.2 whileLoop (while loop)

The while loop runs as long as a condition is true. Once the condition becomes false, the loop stops.

let mut number = 3;

while number != 0 {
    
    
    println!("{}!", number); // {}!
    number -= 1;
}

println!("Blast off!"); // 发射!

Here, the loop will print the numbers 3, 2, 1, and then “Blast off!” once the number reaches 0.

2.3 forLoop (for loop)

The for loop is used to iterate over elements in a collection, such as an array or a range.

let fruits = ["apple", "banana", "cherry"];

for fruit in fruits.iter() {
    
    
    println!("I love {}!", fruit); // 我喜欢{}!
}

for number in (1..4).rev() {
    
    
    println!("{}!", number); // {}!
}

In the first loop, we iterate over an array of fruits and print each fruit. In the second loop, we iterate over a range of numbers in reverse order.

In summary, loops in Rust allow you to execute code multiple times, either indefinitely or based on a condition. They are a powerful tool for creating efficient and repetitive tasks in your programs. In the upcoming chapters, we will delve deeper into more advanced Rust concepts.

Conclusion

In our programming learning journey, understanding is an important step for us to move to a higher level. However, mastering new skills and ideas always requires time and persistence. From a psychological point of view, learning is often accompanied by constant trial and error and adjustment, which is like our brain gradually optimizing its "algorithm" for solving problems.

This is why when we encounter mistakes, we should view them as opportunities to learn and improve, not just as annoyances. By understanding and solving these problems, we can not only fix the current code, but also improve our programming skills and prevent making the same mistakes in future projects.

I encourage everyone to actively participate and continuously improve their programming skills. Whether you are a beginner or an experienced developer, I hope my blog will be helpful on your learning journey. If you find this article useful, you may wish to click to bookmark it, or leave your comments to share your insights and experiences. You are also welcome to make suggestions and questions about the content of my blog. Every like, comment, share and attention is the greatest support for me and the motivation for me to continue sharing and creating.


Read my CSDN homepage and unlock more exciting content: Bubble’s CSDN homepage
Insert image description here

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/133351827