let tup : (i32, f64, u8) = (666, 2.0, 1);
let tup = (666, 2.0, 1);
let (x, y, z) = tup;
let x = tup.0;
let y = tup.1;
let z = tup.2;
let arr = [1, 2, 3];
let sss = ["mon", "tue", "fri"];
let a: [i32; 5]; # 长度为5的整型数组
let b: [3; 5]; # 5个3
let data = a[0];
let s = String::from("hello");
func(&s);
fn func(str: &string) -> ...
let mut s = String::from("hello");
func(&mut s);
fn func(str: &mut String) ->...
注意:作用域结束不是以大括号结束,是以变量最后一次使用结束变量作用域(是没有显式标志)。
例如:
let mut s = String::from("hello");
let r1 = &s; // 没问题
let r2 = &s; // 没问题
println!("{} and {}", r1, r2);
// 此位置之后r1 和r2 不再使用
let r3 = &mut s; // 没问题
println!("{}", r3);
字符串slice:
let slice_name = target_set[start_index .. end_index]
let s = String::from("hello world");
let h = &s[0..5];
let w = &s[6..11];
# 以下几个等效
let h1 = &s[..5];
let w = &s[6..];
let s2 = &s[..];
字符串slice类型声明:&str
fn func(s: &String) -> &str { ...
字符串字面值是slice
s类型是&str
let s = "hello world";
struct Rectangle {
width: i32,
height: i32,
}
# 实例化,不需要new,key: value方式赋值。
let retc = Rectangle {
width: 10,
height: 20,
}
# 不需要大括号定义成员
struct SomeTrait;
# 实例化,不需要括号,没有成员需要赋值
let obj = SomeTrait;
struct User {
a: i32,
b: i32,
c: i32,
}
let u1 = User {
a: 1,
b: 2,
c: 3,
};
# ..u1(u1为已有结构体实例)必须放到最后
# 除明确复制的字段(b),其他字段值使用u1实例的
# 结构体更新语法,移动了数据,对于不支持copy trait的类型,更新后,原实例对象不可用。
let u2 = u1 {
b: 99,
..u1,
}
struct Color {i32, i32, i32};
struct Point {i32, i32, i32};
let bgColor = {0, 0, 0};
let pointA = {1, 2, 3};
#[derive(Debug)]
{:?}
,不能使用{ }
#[derive(Debug)]
struct Rect {
width: i32,
height: i32,
}
fn main() {
let rect = Rect { width: 20, height: 30 };
println!("retc: {:?}\n", rect);
}
Debug
Copy
Clone
Default
Hash
PartialEq, Eq
PartialOrd, Ord
&self
是self: &Self
的缩写# 函数示例
struct Rect {
width: i32,
height: i32,
}
fn area(rect: &Rect) -> i32 {
rect.width * rect.height
}
let r = Rect { width: 20, height: 30, };
let a = area(&r); # 调用函数
struct Rect {
width: i32,
height: i32,
}
# 定义方法,
# 1. 在某个struct类型中:impl Rect { }
# 2. 第一个参数为:&self
# 3. 调用方式,通过实例调用:r.area()
impl Rect {
fn area(&self) -> i32 {
self.width * self.height
}
}
# 多个impl块
impl Rect {
fn isSquare(&self) -> bool {
self.width == self.height
}
}
let r = Rect { width: 20, height: 30, };
let x = r.area(); # 调用方法
let y = r.isSquare();
struct Rect {
w: i32,
h: i32,
}
impl Rect {
fn new (a: i32, b: i32) -> Self {
Self {
w: a,
h: b,
}
} // fn new
} // impl
let r = Rect::new(20, 30);
enum IpAddrKind {
V4,
V6,
}
// 可以替代enum + struct
// 枚举成员可以是各种类型值,不同成员可以是不同类型。
// 枚举类型成员名字,也是一个构建枚举类型实例的函数。
enum IpAddrKind {
V4 (u8, u8, u8, u8),
V6 (String),
}
let four = IpAddrKind::V4;
struct Ipv4Addr {
//...
}
struct Ipv6Addr {
//...
}
// enum成员是struct类型
enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
// 成员多种不同类型
enum Message {
Quit,
Move {x: i32, y:i32, },
Write (String),
ChangeColor(i32, i32, i32),
}
// 在impl块中增加方法定义
impl Message {
fn call(&self) {
// ...
}
}
let m = Message::Write(String::from("hello"));
m.call();
enum Option<T> {
None,
Some(T),
}
let num = Some(5);
let empty: Option<i32> = None;
enum Week {
Mon,
Tue,
Sat,
Sun,
}
fn query(day: Week) -> i32 {
match day {
Week::Mon => 1,
Week::Tue => {
println!("...");
2
// }后面的,是可选的
},
Week::Sat => 6,
Week::Sun => 7,
}
}
struct Ipv4Addr {
//...
}
struct Ipv6Addr {
//...
}
// enum成员是struct类型
enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
// 在match匹配过程中,把值绑定到变量
fn process(data: IpAddr) {
match data {
IpAddr::V4 (addr4) => {
// consume addr4
},
IpAddr::V6 (addr6) => {
// consume addr6
},
}
}
fn func(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i+1),
}
}
let num = Some(12);
let res2 = func(num);
let res3 = func(None);
// 和上面match等价
if let Some(i) = num {
Some(i+1)
} else {
None
}
let result: i32 = func();
match result {
6 => "lucky",
8 => "money",
other => println!("the result: {}", other),
}
match result {
6 => "lucky",
8 => "money",
_ => "ok",
// 或者完全忽略
_ => (),
}