let
关键字immutable
)fn main() {
println!("Hello, world!");
let x: i32=5;
println!("the value of x is {}",x);
}
mut
,就可以使变量可变fn main() {
println!("Hello, world!");
let mut x: i32=5;
x=8;
println!("the value of x is {}",x);
}
constant
),常量在绑定值以后是不可变的,但是让他与不可变的变量有很多区别:
mut
,常量永远都是不可变的const
关键字,它的类型必须被标注Rust
里常量使用全大写字母,每个单词之间用下划线分开,例如:
MAX_POINTS
const MAX_POINTS:u32 = 100_000;
const MAX_POINTS:u32 = 100_000;
fn main() {
println!("Hello, world!");
println!("the number is {}",MAX_POINTS)
}
Shadowing
(隐藏)Shadow
(隐藏)之前申明的同名变量
const MAX_POINTS:u32 = 100_000;
fn main() {
println!("Hello, world!");
println!("the number is {}",MAX_POINTS)
}
Shadow
和把变量标记为 mut
是不一样的:
let
关键字,那么重新给非 mut
的变量赋值会导致编译错误let
申明的同名新变量,也是不可变的let
申明的同名新变量,他的类型可以与之前不同fn main() {
let spaces = " ";
let spaces=spaces.len();
println!("the number is {}", spaces) // 4
}
Rust
是静态编译语言,在编译时必须知道所有变量的类型
String
转为整数的 parse
方法),就必须添加类型的标注,否则编译会报错fn main() {
let guess:i32 = "42".parse().expect("not a number");
println!("number is {}",guess);
}
整数类型没有小数部分
例如 u32
就是一个无符号的整数类型,占据32位空间
无符号整数类型以 u
开头
有符号整数类型以 i
开头
Rust 的整数类型列表如图:
isize
和 usize
isize
和 usize
类型的位数由程序运行的计算机的架构所决定的:isize
和 usize
的主要场景是对某种集合进行索引操作byte
类型外,所有的数值字面值都允许使用类型后缀
57u8
Rust
相应的默认类型i32
f32
,32位,单精度f64
,64为,双精度IEEE-754
标准来表达f64
是默认类型,因为在现代 CPU 上 f64
和 f32
的速度差不多,而且精度更高fn main() {
let sum = 5 + 10;
let difference = 95.5 - 4.3;
let product = 4 * 30;
let quotient = 56.7 / 32.2;
let reminder = 54 % 5;
}
fn main() {
let t = true;
let m: bool = false;
}
U+0000
到 U+D7FF
U+E000
到 U+10FFFF
fn main() {
let x: &str = "z";
let y: char='z';
let z: char='';
}
fn main() {
let tup: (i32, f64, u8) =(500,6.4,1);
println!("{},{},{}",tup.0,tup.1,tup.2);
}
fn main() {
let tup: (i32, f64, u8) =(500,6.4,1);
let (x,y,z) =tup;
println!("{},{},{}",x,y,z);
}
fn main() {
let tup: (i32, f64, u8) =(500,6.4,1);
println!("{},{},{}",tup.0,tup.1,tup.2);
}
fn main() {
let a = [1,2,3,4,5];
}
let a:[i32;5] = [1,2,3,4,5];
let a=[3;5];
相当于 let a = [3,3,3,3,3];
fn main() {
let a = [1,2,3,4,5];
let first = a[0];
let second = a[1];
}
fn main() {
println!("hello world!");
another_function();
}
fn another_function(){
println!("Another function");
}
parameters
、arguments
fn main() {
println!("hello world!");
another_function(5); // argument
}
fn another_function(x:i32){ // parameter
println!(" the number is {}",x);
}
->
符号后边申明函数返回值的类型,但是不可以为返回值命名fn main() {
let x=five(1);
println!("the number is {}",x); // 6
}
fn five(x:i32)->i32{
x+5
}
fn main() {
let num=3;
if num<5{
println!("true");
}else {
println!("false");
}
}
fn main() {
let condition = true;
let num = if condition { 5 } else { 6 };
println!("the number is {}",num); // 5
}
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("the result is {}", result); // 20
}
fn main() {
let mut counter = 3;
while counter != 0 {
println!("number is {}", counter);
counter = counter - 1;
}
println!("LIFTOFF!!!");
}
fn main() {
let a = [10, 20, 30, 40, 50];
let mut index = 0;
while index < 5 {
println!("{}", a[index]);
index += 1;
}
}
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a.iter(){
println!("{}",element);
}
}
fn main() {
for number in (1..4).rev(){
println!("{}",number);
}
println!("LIFTOFF!!!")
}
本文参考书籍:Rust 权威指南
本文参考视频:bilibili-Rust 编程语言入门:https://www.bilibili.com/video/BV1hp4y1k7SV
本文首发于本人博客:https://blog.gitnote.cn/post/rust_learn_003
版权信息: CC BY-NC-SA 4.0 (自由转载-非商用-相同方式共享-保持署名)