关于rust的安装,可以参考官方文档。 因为我的系统为manjaro,所以我选择了使用yay来安装rust。
bashyay -S rust
安装完成后,可以使用rustc --version
来查看rust的版本。
使用cargo来创建一个新的项目。
bashcargo new rust1
在rust中,使用println!
来输出字符串。
rustfn main() {
println!("Hello, world!");
}
使用cargo run
来运行程序。
bashcargo run
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.40s
Running `target/debug/rust1`
Hello, world!
rust的数据类型分为两类:标量和复合。
标量类型包括整型、浮点型、布尔型和字符型。
大小 | 有符号 | 无符号 |
---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
arch | isize | usize |
值得注意的是,rust数据类型和go一样是自动推断的,所以在声明变量的时候,可以不用指定类型。 但是,如果想要指定类型,可以使用
let x: i32 = 5;
来指定类型。
rust Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
error[E0600]: cannot apply unary operator `-` to type `u32`
--> src/main.rs:11:19
|
11 | let x : u32 = -67;
| ^^^ cannot apply unary operator `-`
|
= note: unsigned values cannot be negated
For more information about this error, try `rustc --explain E0600`.
error: could not compile `rust1` due to previous error
rust Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
error: literal out of range for `i8`
--> src/main.rs:11:16
|
11 | let x:i8 = 129;
| ^^^
|
= note: `#[deny(overflowing_literals)]` on by default
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using the type `u8` instead
error: could not compile `rust1` due to previous error
rust Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
i8的最大值: 127
i8的最小值: -128
i16的最大值: 32767
i16的最小值: -32768
i32的最大值: 2147483647
i32的最小值: -2147483648
i64的最大值: 9223372036854775807
i64的最小值: -9223372036854775808
i128的最大值: 170141183460469231731687303715884105727
i128的最小值: -170141183460469231731687303715884105728
rust的浮点型包括f32和f64。
rustlet x:f32 = 889_990_3.0;
println!("x = {}",x);
println!("f32的最大值: {}",std::f32::MAX);
println!("f32的最小值: {}",std::f32::MIN);
println!("f64的最大值: {}",std::f64::MAX);
println!("f64的最小值: {}",std::f64::MIN);
如上,我们可以使用_
来分隔数字,使得数字更加易读。
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = 8899903
f32的最大值: 340282350000000000000000000000000000000
f32的最小值: -340282350000000000000000000000000000000
f64的最大值: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
f64的最小值: -179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
rust的布尔型只有true和false。
rustlet x = true;
let y:bool = false;
println!("x = {},y = {}",x,y);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = true,y = false
rust的字符型是char
,它的长度是4个字节。也就是单个字符或者汉字的长度。
使用utf-8编码。
rustlet x = 'a';
let y:char = 'b';
println!("x = {},y = {}",x,y);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = a,y = b
元组是一个有序的列表,可以存储不同类型的数据。
rustlet x = (1,2,3);
let y = (1,"hello",true);
println!("x = {:?}",x);
println!("y = {:?}",y);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = (1, 2, 3)
y = (1, "hello", true)
数组是一个有序的列表,可以存储相同类型的数据。
rustlet x = [1,2,3];
let y = [1,2,3,4,5];
println!("x = {:?}",x);
println!("y = {:?}",y);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = [1, 2, 3]
y = [1, 2, 3, 4, 5]
rust的字符串是String
类型,它是一个可变的字符串。
rustlet mut x = String::from("hello");
x.push_str(" world");
println!("x = {}",x);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = hello world
rust的变量声明使用let
关键字。
rustlet x = 1;
let y:i32 = 2;
println!("x = {},y = {}",x,y);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = 1,y = 2
rust的变量默认是不可变的,如果想要修改变量的值,需要使用mut
关键字。
rustlet mut x = 1;
x = 2;
println!("x = {}",x);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = 2
rust的变量可以隐藏,隐藏的变量会覆盖之前的变量。
rustlet x = 1;
let x = 2;
println!("x = {}",x);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = 2
rust的常量使用const
关键字声明。
rustconst X:i32 = 1;
const Y:i32 = 2;
println!("x = {},y = {}",X,Y);
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
x = 1,y = 2
rust的函数使用fn
关键字声明。
rustfn add(x:i32,y:i32) -> i32{
x + y
}
fn main() {
let x = 1;
let y = 2;
let z = add(x,y);
println!("z = {}",z);
}
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
z = 3
rust的函数参数可以有默认值。
rustfn add(x:i32,y:i32 = 2) -> i32{
x + y
}
fn main() {
let x = 1;
let y = 2;
let z = add(x);
println!("z = {}",z);
}
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
z = 3
rust的函数可以有返回值,如果没有返回值,可以使用()
表示。
rustfn add(x:i32,y:i32) -> i32{
x + y
}
fn main() {
let x = 1;
let y = 2;
let z = add(x,y);
println!("z = {}",z);
}
输出结果:
bash Compiling rust1 v0.1.0 (/home/duty/rust/rust1)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/rust1`
z = 3
rust的注释有两种,一种是单行注释,一种是多行注释。
单行注释使用//
,多行注释使用/* */
。
rust//单行注释
/*
多行注释
*/
运算符 | 描述 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
% | 取余 |
运算符 | 描述 |
---|---|
== | 等于 |
!= | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
运算符 | 描述 |
---|---|
&& | 逻辑与 |
|| | 逻辑或 |
! | 逻辑非 |
运算符 | 描述 |
---|---|
= | 赋值 |
+= | 加赋值 |
-= | 减赋值 |
*= | 乘赋值 |
/= | 除赋值 |
%= | 取余赋值 |
运算符 | 描述 |
---|---|
& | 按位与 |
| | 按位或 |
^ | 按位异或 |
~ | 按位取反 |
<< | 左移 |
>> | 右移 |
运算符 | 描述 |
---|---|
. | 成员访问 |
-> | 成员访问 |
:: | 作用域 |
: | 类型标注 |
@ | 模式匹配 |
.. | 范围 |
..= | 范围 |
... | 范围 |
, | 分隔符 |
; | 分隔符 |
=> | 键值对 |
-> | 返回类型 |
=> | 闭包 |
-> | 闭包 |
本文作者:独语天涯
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!