基于Steve Klabnik的《The Rust Programming Language》一书。昨天我们做了一个猜谜游戏 ,今天我们将探讨常见的编程概念,例如:
- Variables 变量
- Constants 常数
- Shadowing 阴影
- Data Types 数据类型
- Functions 功能
Variables 变量
In layman terms, Variables are like container that store a value. Let’s just take a quick look at them
通俗地说,变量就像存储值的容器。让我们快速的看一下
- Variables can store different types of values such as : numbers, text or complex structures
变量可以存储不同类型的值,例如:数字、文本或复杂结构 - Each variable has a name
每个变量都有一个名称 - In rust a variable is immutable by default, meaning the value cannot be change once set, to make a variable mutable
mut
keyword is used.
在rust中,变量默认是不可变的,这意味着一旦设置了值就不能改变,要使变量可变,使用mut
关键字。 - You can explicitly define what type of value is going to be stored in a variable but it is optional.
你可以显式定义什么类型的值将被存储在变量中,但它是可选的。 - Variables have a limited scope, meaning they can only be accessed in a certain portion of the code
变量的作用域有限,这意味着只能在代码的某个部分访问它们
Example : 范例:
fn main(){let x = 10; //immutable varlet mut y = 15; //mutable varprintln!("X = {}",x);println!("Y = {}",y);y = 2println!("X = {}",x);println!("Y = {}",y);
}
Constants 常数
Like immutable variables, constants are values that are not allowed to change, but there are a few differences
与不可变变量一样,常量是不允许更改的值,但也有一些不同之处
- Constants are inherently immutable,
mut
keyword cannot be used with them
常量本质上是不可变的,mut
关键字不能与它们一起使用 - They are initialized at the time of compilation, compile-time initialization
它们在编译时初始化,编译时初始化 - Constants have a global scope and can be accessed from anywhere in the program
常量具有全局作用域,可以从程序中的任何位置访问 - Type definition is required for constants
常量需要类型定义
Example : 范例:
const VALUE_OF_PI: u32 = 3.1418;
Shadowing 阴影
As we saw yesterday, with shadowing you can declare a new variable with the same name as the previous variable and the newer variable will overshadow the previous one
正如我们昨天所看到的,使用阴影,您可以声明一个与前一个变量同名的新变量,并且新变量将覆盖前一个变量
Example : 范例:
fn main(){let x = 10;let x = x - 7;{let x = x * 2;println!("X in inner scope = {}",x);}println!("X in oouter scope = {}",x);
}
Output : 输出量:
$ cargo run
X in inner scope = 6
X in outer scope = 3
Data Types 数据类型
Data types can further be divided into two groups: Scalar and Compound data types
数据类型可以进一步分为两组:标量和复合数据类型
Scalar Data Types 标量数据类型
A scalar type represents one value, Rust has 4 primary Scalar data types:
标量类型代表一个值,Rust有4种主要的标量数据类型:
Integer 整数
An integer is a number without the fractional component, We used it yesterday in the guessing game, u32
means an unsigned 32 bit integer
整数是一个没有小数部分的数字,我们昨天在猜谜游戏中使用了它, u32
表示无符号的32位整数
Other types of integer include :
其他类型的整数包括:
Signed : 签署人:
- The difference between a signed and an unsigned integer is whether the number needs to have a
sign
or whether it will only be positive
有符号整数和无符号整数之间的区别在于数字是否需要有一个sign
,或者它是否只会是正数 - Each signed integer can store numbers from -(2^(n — 1)) to (2^(n — 1)) — 1, Where n = the number of bits the variant uses
每个有符号整数可以存储从-(2^(n - 1))到(2^(n - 1))- 1的数字,其中n =变体使用的位数 - Example : i8, i16, i32, i64, i128
示例:i8、i16、i32、i64、i128
Unsigned : 未签名:
- Unsigned numbers are always positive.
无符号数总是正数。 - Each unsigned integer can store numbers from 0 to
2^n — 1
每个无符号整数可以存储从0到2^n — 1
的数字 - Example : u8, u16, u32, u64, u128
示例:u8、u16、u32、u64、u128
Additionally there is isize
and usize
depend on the architecture of the computer the program is running on. 32 bits if the program is running on 32 bit architecture and 64 bits if the program is running on 64 bit architecture
此外,还有 isize
和 usize
取决于程序运行的计算机的架构。如果程序运行在32位架构上,则为32位,如果程序运行在64位架构上,则为64位
Integer Overflow: 溢出:
- Computers represent integers using a fixed number of bits, determining the range they can cover.
计算机使用固定的位数来表示整数,确定它们可以覆盖的范围。 - In Rust, when the result of an arithmetic operation exceeds the maximum representable value for a data type (overflow), it wraps around to the minimum value and continues counting.
在Rust中,当算术运算的结果超过数据类型的最大可表示值(溢出)时,它会返回到最小值并继续计数。
Example of Integer overflow:
内存溢出示例:
If you’re using an 8-bit integer, the range is 0 to 255. If you try to add 1 to 255, in normal arithmetic, you’d expect 256. However, in Rust, it wraps around to 0 due to overflow.
如果使用8位整数,则范围为0到255。如果你试着把1加到255,在正常的算术中,你会得到256。然而,在Rust中,由于溢出,它会返回到0。
Floating-point numbers : 浮点数:
Floating point numbers in rust have a fractional component. Unlike integers Floating-point numbers are always signed. There are two types of floating-point numbers : f32
and f64
32 and 64 representing the size in bits.
rust中的浮点数有一个小数部分。与整数不同,浮点数总是有符号的。有两种类型的浮点数: f32
和 f64
32和64表示位的大小。
Example : 范例:
fn main() {// Using f32let float_32: f32 = 3.14; // A 32-bit floating-point numberprintln!("f32 value: {}", float_32);// Using f64let float_64: f64 = 3.141592653589793; // A 64-bit floating-point numberprintln!("f64 value: {}", float_64);// Performing arithmetic operationslet result_f32 = float_32 * 2.0;let result_f64 = float_64 * 2.0;// Displaying resultsprintln!("Result (f32): {}", result_f32);println!("Result (f64): {}", result_f64);
}
Numeric operations 数值运算
Rust supports the basic mathematical operations such as :
Rust支持基本的数学运算,例如:
- Addition 此外
- Subtraction 减法
- Multiplication 乘法
- Division 司
- Modulus 模量
Example: 范例:
fn main(){let sum = 10+7;let subtraction = 60-4;let multilpication = 7 * 7;let division = 25/5;let modulus = 13%2;println!("10 + 7 = {}",sum);println!("60 - 4 = {}",subtraction);println!("7 * 7 = {}",multilpication);println!("25 / 5= {}",division);println!("13 % 2= {}",modulus);
}
Boolean Type 布尔类型
As in most programming languages, a boolean type is one byte in size and can be either true
or false
.
与大多数编程语言一样,布尔类型的大小是一个字节,可以是 true
或 false
。
Example: 范例:
fn main(){let t = true;let f: bool = false;
}
Character type 字符类型
Rust’s char type is the most primitive alphabetic type, similar to char
in C.
Rust的char类型是最原始的字母类型,类似于C中的 char
。
fn main(){let z = 'Z';let x: char = "X";
}
Side note : Characters are enclosed in single quotes, and Strings are enclosed in double quotes.
旁注:字符用单引号括起来,字符串用双引号括起来。
Compound Types 复合类型
Compound types can store multiple values in one type. They are often group of scalar data.
复合类型可以在一个类型中存储多个值。它们通常是一组标量数据。
Tuple 元组
A tuple in Rust is like a mixed bag that can hold different types of items, allowing you to group and organize them in a specific order. It’s a simple way to bundle multiple values together.
Rust中的元组就像一个混合的袋子,可以容纳不同类型的项目,允许您以特定的顺序对它们进行分组和组织。这是一种将多个值捆绑在一起的简单方法。
Tuples are immutable. However, the elements within the tuple may be mutable if they are declared as mutable variables.
元组不可变。但是,如果元组中的元素被声明为可变变量,则它们可能是可变的。
Syntax 语法
let var_name: (data_type1,data_type2...data_typeN) = (val1,val2...valN);//The values in this tuple is mutable
let mut var_name: (data_type1,data_type2...data_typeN) = (val1,val2...valN);
Example 例如
let tup: (i32, f64, u16) = (500,3.141,9);
Accessing data in tuples 将数据存储在元组中
Indexing: 索引:
Indexing is the act of accessing a specific element in a data structure, like an array or tuple, using its position or key.
索引是访问数据结构中特定元素的行为,如数组或元组,使用其位置或键。
To access a tuple using indexing we can do this:
要使用索引访问元组,我们可以这样做:
println!("{}",tuple_name.index);
Example: 范例:
fn main() {// Creating a tuplelet my_tuple = (42, "hello", 3.14);// Accessing elements using indexing (zero-based)let first_element = my_tuple.0; // Access the first element (42)let second_element = my_tuple.1; // Access the second element ("hello")let third_element = my_tuple.2; // Access the third element (3.14)// Printing the accessed elementsprintln!("First element: {}", first_element);println!("Second element: {}", second_element);println!("Third element: {}", third_element);
}
Array 阵列
Arrays are another way to store data of the same type, sequentially. Arrays in rust have a fixed length.
数组是按顺序存储相同类型数据的另一种方式。rust中的数组有固定的长度。
To write an array we use square brackets []
as a comma-seperated list.
为了写一个数组,我们使用方括号 []
作为逗号分隔的列表。
Example: 范例:
fn main(){let a: [i32;5]= [1,2,3,4,5];
}
We can also do this :
我们也可以这样做:
fn main(){let x = [6; 4]; // [6,6,6,6]
}
Accessing array elements:
数组元素:
fn main() {let a: [u32; 6] = [1, 2, 3, 4, 5, 6];let first: u32 = a[0];let last: u32 = a[a.len() - 1];println!("First element : {}", first); // 1println!("Last element : {}", last); // 6
}
Functions 功能
Technically the definition of a functions is, a self-contained unit of code that performs a specific task, taking inputs and producing outputs, enhancing code structure and reusability.
从技术上讲,函数的定义是一个独立的代码单元,它执行特定的任务,接受输入并产生输出,增强代码结构和可重用性。
The main
function is the entry point of a rust porgram, similarly we can use the fn
keyword to declare custom functions that perform a specific task.main
函数是rust程序的入口点,同样,我们可以使用 fn
关键字来声明执行特定任务的自定义函数。
In rust snake_case
is the convention for declaring functions and variable names.
在rust中, snake_case
是声明函数和变量名的约定。
Example 例如
fn main(){println!("The main Function");new_function();
}fn new_function(){println!("This is a custom function");
}
Output 输出
$ cargo run
The main Function
This is a custom function
Parameters 参数
In layman’s terms : A parameter is an input passed to a function
通俗地说:参数是传递给函数的输入
Technically, Parameters are a function’s placeholders in its definition and arguments are the actual values or data you provide to a function when you call it. They match the parameters’ types and order defined in the function.
从技术上讲,参数是函数定义中的占位符,参数是调用函数时提供给函数的实际值或数据。它们与函数中定义的参数类型和顺序相匹配。
Although people do tend to use these words interchangeably.
尽管人们倾向于互换使用这些词。
Example 例如
fn main(){println!("The main Function");addition(5,9);
}
fn addition(x: i32,y: i32){println!("Addition of {} + {} = {}",x,y,x+y);
}
Output 输出
$ cargo run
The main Function
Addition of 5 + 9 = 14
Statements and expressions
语句和表达式
Statements are instruction that perform a certain task, Whereas Expressions evaluate to a value.
语句是执行特定任务的指令,而表达式的计算结果是一个值。
Example 例如
fn main(){let y = 6; //Statementlet x = 5; //Statementlet result = x+y; // here "x+y" is an expression
}
Functions with return values
返回值函数
Functions can return a value when they are called . We must declare the return type of a function with an arrow ->
函数在被调用时可以返回一个值。我们必须用箭头声明函数的返回类型 ->
In rust, The final expression of the function is the return value of the function.
在rust中,函数的最终表达式是函数的返回值。
Example 例如
fn main(){println!("The main Function");let sum = addition(5,9);println!("Result of the function : {}", sum);
}
fn addition(x: i32,y: i32) -> i32 {x+y // The return statement
}
In Rust, the absence of a semicolon at the end of a return statement indicates that it is the value to be returned, making the code more explicit and reducing redundancy.
在Rust中,return语句末尾没有一个numberon表示它是要返回的值,使代码更加明确并减少冗余。