【Rust 控制流入门指南】 Introduction to Control Flow in Rust

简介: 【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 if Statement (if 语句)

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 if Statement (else if 语句)

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 else Statement (else 语句)

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 循环)

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 while Loop (while 循环)

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 for Loop (for 循环)

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.

结语

在我们的编程学习之旅中,理解是我们迈向更高层次的重要一步。然而,掌握新技能、新理念,始终需要时间和坚持。从心理学的角度看,学习往往伴随着不断的试错和调整,这就像是我们的大脑在逐渐优化其解决问题的“算法”。

这就是为什么当我们遇到错误,我们应该将其视为学习和进步的机会,而不仅仅是困扰。通过理解和解决这些问题,我们不仅可以修复当前的代码,更可以提升我们的编程能力,防止在未来的项目中犯相同的错误。

我鼓励大家积极参与进来,不断提升自己的编程技术。无论你是初学者还是有经验的开发者,我希望我的博客能对你的学习之路有所帮助。如果你觉得这篇文章有用,不妨点击收藏,或者留下你的评论分享你的见解和经验,也欢迎你对我博客的内容提出建议和问题。每一次的点赞、评论、分享和关注都是对我的最大支持,也是对我持续分享和创作的动力。

目录
相关文章
|
7天前
|
Rust 编译器
Rust的Match语句:强大的控制流运算符
Rust的Match语句:强大的控制流运算符
9 0
|
8天前
|
Rust 开发者
Rust函数入门与函数重载
Rust函数入门与函数重载
6 0
|
2月前
|
Web App开发 Rust 安全
一名C++程序员的Rust入门初体验
作者最近尝试写了一些Rust代码,本文主要讲述了对Rust的看法和Rust与C++的一些区别。
155 3
|
2月前
|
Rust 安全
Rust语言中的控制流:条件语句、循环与模式匹配详解
本文将深入探讨Rust编程语言中的控制流构造,包括条件语句、循环和模式匹配。我们将了解如何使用这些工具来构建高效、可读和安全的代码。此外,我们还将探讨Rust在这些构造中提供的一些独特功能和优化。
|
2月前
|
Rust 测试技术
【Rust】——控制流(if-else,循环)
【Rust】——控制流(if-else,循环)
46 0
|
8月前
|
Rust
Rust 基础入门 —— 语句与表达式
语句与表达式 这一节,我们接触的是rust中的有一个基本类型 我将其称之为 —— 逻辑结构,这个是我自己命名的,但我觉得很贴切。
40 3
|
8月前
|
Rust Go
Rust 基础入门 —— 字符、布尔、单元 类型
布尔类型(bool) 说明一点,bool类型的应用场景 主要就是用在流程控制中,
62 2
|
8月前
|
存储 Rust JavaScript
Rust 基础入门 —— 基本类型
当然,作为强类型,也不一定要全部推导,可以通过对变量进行类型标注的操作,完成对类型的显式说明,通过这样的方式就可以减小推导时间,特别是在复合类型中应用这样的方式。
31 1
|
8月前
|
存储 Rust 程序员
Rust 基础入门 —— 变量绑定与解构
首先讨论的第一点,就是对于传统的开发者来说明:为什么要去用 这样手动设定的方式设定变量的可行性。
64 0
|
8月前
|
Rust 安全 编译器
Rust 基础入门 —— 2.3.所有权和借用 (二)
同一时刻,你只能拥有要么一个可变引用, 要么任意多个不可变引用 引用必须总是有效的 贴一个体验不错的学习链接恰饭:学习链接
52 0