Rust 学习笔记
Rust 学习笔记
[
variables:变量
mutable:可变的
compile:编译
unoptimized:未优化
mismatched:搭配不当
scalar:标量
compound:复合类型
parse:从语法上分析
annotation:注解,标注
type annotations needed
infer:推断,推理
panic:惊慌,恐慌
Wrapping:环绕
]
COMM PROGRAMMING CONCEPTS
{变量、基本类型、函数、注释和控制流}
KEYWORDS
关键字
Appendix A
附录A
Variables and Mutability
变量与可变性
Rust中的变量默认是不可变的。
Error: cannot assign twice to immutable variable
let x = 5;
let mut y = 6;
mut变量不可以更改数据类型
常量 constant
const MAX_POINTS: u32 = 100_000;
隐藏 shadow
fn main() {
let x = 5;
let x = x + 1;
let x = x * 2;
let spaces = " ";
let spaces = spaces.len(); //changed types
println!("The value of x is: {}", x); //12
println!("The length of spaces is: {}", spaces); //4
}
Data Types
数据类型
let guess: u32 = “42 is a number?”.parse().expect(“Not a number!”);
Scalar Types
标量类型
Integer Types
整数类型
Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
arch isize usize
Integer Literals in Rust
整数字面量
Number literals Example
Decimal 98_222
Hex 0xff
Octal 0o77
Binary 0b1111_0000
Byte(u8 only) b’A’
Interger Overflow
整数溢出
Floating-Point Types
浮点数类型
fn main() {
let x = 2.0; //f64
let y: f32 = 3.0 //f32
println!("The x,y is: {},{}", x, y); //2.0,3.0
}
Numberic Operations
数值运算
fn main() {
//addition
let sum = 5 + 10;
//subtration
let difference = 95.5 - 4.3;
//multiplication
let product = 4 * 30;
//division
let quotient = 56.7 / 32.2;
remainder
let remainder = 43 % 5;
println!("The sum is: {}", sum);
println!("The difference is: {}", difference);
println!("The product is: {}", product);
println!("The quotient is: {}", quotient);
println!("The remainder is: {}", remainder);
}
The Boolean Type
布尔类型
fn main() {
let t = true;
//with explicit type annotation
let f: bool = false;
println!("true or false:{} {}", t, f);
}
The Character Type
字符类型
fn main() {
let x = 'A';
let y = '①';
let z = '©';
println!("The character x,y,z is {} {} {}", x, y, z);
}
Compound Types
复合类型
The Tuple Type
元组类型
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The x, y, z is {}, {}, {}", x, y, z);
let five_handred = tup.0;
let six_point_four = tup.1;
let one = tup.2;
println!("The five_handred equals {}", five_handred);
println!("The six_point_four equals {}", six_point_four);
println!("The one equals {}", one);
}
The Array Type
数组类型
fn main() {
let x = [1, 2, 3, 4, 5];
let y: [f64; 5] = [0.618, 11.11, 3.14, 4.05, 5.21];
let z = [3; 5];
let index = 2;
let months = ["January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December"];
let element = months[index];
println!("The array x is {:?}", x);
println!("The array y is {:?}", y);
println!("The array z is {:?}", z);
println!("The value of element is {}", element);
}
Function
函数(snake case)
fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}
Function Parameters
函数参数
fn main() {
println!("Hello, world!");
another_function(5, 6);
}
fn another_function(x: i32, y: i32) {
println!("The value of x is: {}.", x);
println!("The value of y is: {}.", y);
println!("The value of x + y is: {}.", x + y);
}
Statements and Expressions in Function Bodies
函数体中的语句和表达式
fn main() {
let x = 5;
let y = {
let x = 1;
x + 1
};
let z = {
let x = 1;
x - 1
};
println!("The value of x is {}.", x);
println!("The value of y is {}.", y);
println!("The value of z is {}.", z);
}
Functions with Return Values
函数的返回值
fn main() {
let x = five();
println!("The value of x is: {}", x);
}
fn five() -> i32 {
5
}
fn main() {
let x = plus_one(5);
println!("The value of x is: {}", x);
}
fn plus_one(x: i32) -> i32 {
x + 1
}
Comments
注释
fn main() {
let lucky_number = 7;
//I'm feeling lucky today.
println!("The value of lucky_number is {}.", lucky_number);
}
Control Flow
控制流
if Expressions
if表达式
fn main() {
let number = 3;
if number < 5 {
println!("The condition was true");
} else {
println!("The condition was false");
}
}
Rust will not automatically try to convert non-Boolean types to a Boolean.
Rust不会自动尝试将非布尔型的值转换为布尔类型。
fn main() {
let number = 3;
if number != 0 {
println!("The number was something other than zero");
}
}
Handling Multiple Conditions with else if
使用else if实现多重条件判断
fn main() {
let number = 6;
if number % 4 == 0 {
println!(" number is divisible by 4");
} else if number % 3 == 0 {
println!(" number is divisible by 3");
} else if number % 2 == 0 {
println!(" number is divisible by 2");
} else {
println!(" number is not divisible by 4, 3, or 2");
}
}
Using if in a let Statement
在let语句中使用if
fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
println!("The value of number is: {}", number);
}
Repetition with Loops
使用循环重复执行代码
Repeating Code with loop
使用loop重复执行代码
Returning Values from Loops
从loop循环中返回值
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The value of counter is {}", counter);
println!("The value of result is {}", result);
}
Conditional Loops with while
while循环
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number = number - 1;
}
println!("LIFTOFF!");
}
Looping Through a Collection with for
使用for来循环遍历集合
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is : {}.", element);
}
}
nicer
fn main() {
for number in (1.. 4). rev() {
println!("{}!", number);
}
println!(" LIFTOFF!!!");
}
本作品采用《CC 协议》,转载必须注明作者和本文链接