let i = 1
fn main() {
let mut i = 3;
println!("{}", i);
i =10 ;
let i = 20;
println!("{}", i);
}
const MAX_POINT: u32 = 10000;
i8、i16、i32、i64、u8、u16、u32、u64、f32、f64
let [Type;Size]
let x:[i32;4] = [1, 2, 3, 4];
println!("{:?}", x);
Size 也是数组类型的一部分
isize、usize
let t1 = (1, 2, 3, 4);
let t2 = ("周", 1, '哈');
println!("{:?}", t1);
println!("{:?}", t2);
let (x,y,z) = t2;
println!("{}", x);
fn main() {
let age = 20;
if age > 60 {
println!("老年人")
}else if age > 18 {
println!("成年人");
}else {
println!("未成年人")
}
}
let x = if true {5 } else { 6 };
loop循环
fn main() {
let mut i = 0;
let res = loop {
if(i==10){
break i;
}
i+=1;
println!("in loop")
};
println!("{}", res);
}
while循环
fn main() {
let mut i = 0;
while i != 10 {
i+=1;
println!("loop in");
}
}
for循环
fn main() {
let arr = [1, 2, 3, 4, 5];
for element in arr {
println!("{}", element);
}
}
函数参数类型不可自动推导
函数返回值类型不可自动推导
fn main() {
println!("{:?}", other_fun1(1, 3));
}
fn other_fun(){
println!("this is a function");
}
fn other_fun1(a:i32, b:i32) -> i32 {
return a+b;
}
fn other_fun2(a:i32, b:i32) -> i32 {
a+b
}
语句
fn main() {
let i = 10;
let x = {
let y = 30;
i + y
};
println!("{}", x);
}
rust通过所有权机制来管理内存,编译器在编译就会根据所有权规则对内存的使用进行检查
堆和栈
编译的时候数据的类型大小是固定的,就是分配在栈上的
编译的时候数据 类型大小不固定,就是分配堆上的
作用域
{}
离开作用域调用drop方法
String内存回收
移动
fn main() {
// 移动 始终只有一份有效
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);//s1 无效
}
clone
fn main() {
// 克隆 深度拷贝
let s1 = String::from("hello");
let s2 = s1.clone();
println!("{}", s1);
}
栈上数据拷贝(栈上移动)
fn main() {
let a = 1;
let b = a;
println!("{}", a);
println!("{}", b);
}
copy trait
只要实现copy 特质,可以继续使用
具有copy特质的有哪些?
所有的数字类型
布尔类型
字符类型
元组
函数和作用域
fn main() {
let i = 8;
printNum(i);
println!("{}", i);
let str:String = String::from("helloworld");
printString(str);
println!("{}", str);
}
fn printString(str: String) {
println!("{}", str);
}
fn printNum(num:i32) {
println!("{}", num);
}
用法 : &
让我们创建一个指向值的引用,但是并不拥有它,因为不拥有这个值,所以,当引用离开其指向的作用域后也不会被丢弃
借用
fn main() {
let mut str:String = String::from("helloworld");
printString(&mut str);
println!("{}", &str);
}
fn printString(str: &mut String) {
str.push_str(" zhwenga");
println!("{}", str);
}
悬垂引用 作用域内内存已被销毁,但返回了引用
fn main() {
let refs = dangle();
}
fn dangle()-> &String{
let string = String::from("hello");
&string
}
在任意给定时间,有了可变引用之后不能再有不可变引用
字符串slice是String中一部分值的引用
字面值就是slice
其他类型slice
fn main() {
let s = String::from("hello world");
let h = &s[0..5];
let h = &s[..5];
let h = &s[6..];
let h = &s[6..=10];
let h = &s[..];
println!("{}", h);
let arr = [1, 2, 3, 4];
let x = &arr[1..3];
println!("{:?}", x);
}
fn main() {
// 1 定义结构体
#[derive(Debug)]
struct User{
name:String,
count:String,
nonce:u64,
active:bool
}
// 2 创建结构体实例
let mut zhangsan = User {
name: String::from("张三"),
count: String::from("23442432"),
nonce: 1000,
active: true
};
// 3 修改结构体字段
zhangsan.count = String::from("3424324242111");
// 4 参数名字和字段同名的简化写法
let name = String::from("李四");
let count = String::from("34243224242");
let nonce = 323434;
let active = true;
let lisi = User {
name,
count,
nonce,
active
};
println!("{:?}", lisi);
// 5 从其他的结构体创建实例
let wangwu = User {
name: String::from("王五"),
..lisi
};
println!("{} {}", wangwu.name,wangwu.active);
// 6 元组结构体(字段没有名字,圆括号)
struct Point(i32,i32);
let a = Point(10, 32);
let b = Point(100, 320);
let c = a.0 + b.1;
println!("{}", c);
// 7 单位结构体
struct unit{}
// 8 打印结构体
println!("{:#?}", wangwu);
}
fn main() {
let dog = Dog {
name: String::from("旺财"),
weight: 32.0,
height: 23.2
};
println!("{:?}", dog);
println!("{}", dog.get_height());
}
#[derive(Debug)]
struct Dog{
name:String,
weight:f32,
height:f32
}
impl Dog{
fn get_name(&self) -> &str{
&(self.name[..])
}
fn get_weight(&self) -> &f32{
&(self.weight)
}
}
impl Dog{
fn get_height(&self) -> &f32{
&(self.height)
}
}
fn main() {
// 1 类似与c语言的方式定义
#[derive(Debug)]
enum IpAddKind{
V4,
V6
}
#[derive(Debug)]
struct IpAddr{
kind:IpAddKind,
address:String
}
let ipv4 = IpAddr {
kind: IpAddKind::V4,
address: String::from("192.168.0.0")
};
println!("{:?}", ipv4);
// 2 rust语言提倡的方式定义
// 3 可以是不同类型
#[derive(Debug)]
enum IpAddr2{
V4(u8,u8,u8,u8),
V6(String)
}
let ip2V4 = IpAddr2::V4(192,168,0,0);
println!("{:?}", ip2V4);
let ip2V6 = IpAddr2::V6(String::from("234,432,432,432,43"));
println!("{:?}", ip2V6);
// 4 经典用法
#[derive(Debug)]
enum Message{
Quit,
Move{x:i32,y: i32},
Write(String),
Change(i32,i32,i32)
}
// 5 枚举类型的方法以及match
impl Message{
fn prin(&self){
match *self {
Message::Quit => {println!("Quit")}
Message::Move{x,y} => {println!("move {}{}",x,y)}
Message::Write(_) => {println!("write")}
Message::Change(_, _, _) => {println!("change")}
_ => println!("Write")
}
}
}
let message = Message::Quit;
message.prin();
let moVe = Message::Move { x: 10, y: 20 };
moVe.prin();
}
fn main() {
let some_number = Some(5);
let some_string = Some(String::from("hello world"));
let absent_number:Option<i32> = None;
match some_number {
None => {}
Some(i) => {
println!("{}", i);
}
}
}
fn main() {
// 1 创建空的vector
let mut vec: Vec<i32> = Vec::new();
// 2 创建包含初始值的vector
let mut vec1 = vec![1, 2, 3];
//3 丢弃vector
// vec1.pop();
// 4 读取元素
// 5 遍历
// 6 使用枚举
vec1.push(32);
for x in &vec1 {
println!("{}", x);
}
match vec1.get(2) {
None => {}
Some(i) => {
println!("{}", i);
}
};
println!("{}", &vec1[0]);
// 可变遍历
for x in &vec1 {
println!("{}", (*x + 2));
}
}
fn main() {
// 1 创建一个空String
let mut str = String::new();
str.push_str("hello");
println!("{}", str);
// 2 通过字面值创建一个String
// 使用 String::from()
// 使用 str
let s1 = String::from("init some thing");
println!("{}", s1);
let mut s1 = "init some ".to_string();
s1.push_str("heee");
println!("{}", s1);
// 3 更新 String
let s1 = "hello".to_string();
let s2 = String::from(",world");
// let x = s1 + &s2;
// println!("{}",x);
let res = format!("{}{}", s1, s2);
println!("{}", s1);
println!("{}", s2);
println!("{}", res);
// 4 String 索引
// 5 str 索引
// 6 chars bytes
let chars = "中国".chars();
for x in chars {
println!("{}", x);
}
for x in "万里长城".bytes() {
println!("{}", x);
}
}
fn main() {
// 1 HashMap
// 2 创建HashMap
// 3 读取
// 4 遍历
// 5 更新
let mut map: HashMap<String,String> = HashMap::new();
map.insert(String::from("name"),String::from("张三"));
map.insert(String::from("name"),String::from("lisi"));
// 不存在 则插入
map.entry(String::from("one")).or_insert(String::from("32"));
println!("{:?}", map);
let name = map.get(&String::from("name"));
match name {
None => {}
Some(name) => {
println!("{}", name);
}
}
for (key, value) in &map {
println!("{} {}", key,value);
}
}
同一文件中
mod factory {
pub mod produce_refrigerator {
pub fn produce() {
println!("生产冰箱")
}
}
mod produce_washing {
fn produce() {
println!("生产洗衣机");
}
}
}
fn main() {
factory::produce_refrigerator::produce();
}
导入外部库的使用
Cargo.toml
[package]
name = "rustPro"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rust-crypto = "0.2"
use crypto::digest::Digest;
use crypto::sha3::Sha3;
fn main() {
let mut hasher = Sha3::sha3_256();
hasher.input_str("hello world");
let resutl = hasher.result_str();
println!("{}",resutl);
}
fn main() {
let f = File::open("hello.txt");
let file1 = match f {
Ok(file) => { file }
Err(error) => { panic!("{}", error) }
};
}