rust18.模块
12//前面有3节模块1,模块2,模块3的内容没有保存...
模块4
123456789//cargo.toml[package]name = "learn"version = "0.1.0"authors = ["andy"]edition = "2018"[dependencies]rust-crypto = "0.2" //使用外部库
使用这个库12345678910111213extern crate crypto;use crypto::digest::Digest;use crypto::sha3::Sha3;fn main() { let mut hasher = Sha3::sha3_256(); hasher.input_str("hello world"); let result = hasher.result_str(); println!("hash == {}",re ...
rust17.hashmap
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263//1.HashMap<K,V>//2.创建HashMap//3.读取//4.遍历//5.更新us std::collections::HashMap;fn main() { //2. let mut scoures: HashMap<String,i32> = HahsMap::new(); scores.insert(String::from("Blue"),10); scores.insert(String::from("Red"),20); let keys = vec![String::from("Blue"),String::from("Red")]; let value = vec![10,20 ...
rust16.字符串string
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192{% note info %}实际上rust核心中一般使用str类型,很少出现string,这种是在编译库中实现的{% endnote%}//1.创建一个空string//2.通过字面值创建一个string//2.1 使用string::from()//2.2 使用str的方式//3. 更新string//3.1 push_str//3.2 push//3.3 使用"+"合并字符串//3.4 使用format//4. string索引//5. str索引//6. 遍历//6.1 chars//6.2 bytesfn main() { //1. let mut s ...
rust15.vector
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071//1.创建空的vector: Vec<T>//2.创建包含初始值的vector//3.丢弃vector//4.读取元素//5.更新//6.遍历//7.使用枚举fn main() { //1.创建 let mut v: Vec<i32> = Vec::new(); //创建变量和之前一样,如果是不可变变量,就不能够改变了 //下面的会报错 //let v: Vec<i32> = Vec::new(); //vc.push(1); vec.push(1); //2. let v = vec![1,2,3]; //3. { let v1 = vec![1,2,3]; } ...
rust14.Option
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647//Option是标准库定义的一种枚举类型//enum Option<T> {// Some(T),// None,// }//match 必须匹配所有的情况//2. 使用方式fn main() { let some_number = Some(5); let some_string = Some(String::from("a string")); let absent_number: Option<i32> = None; let x: i32 = 5; let y: Option<i32> = Some(5); //不能使用,因为x和y类行不同 //let sum = x + y; let mut temp = 0; match y { Some(i) => {te ...
rust13.枚举和模式匹配
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 //这一部分应当放在函数体内部,因为有分号,此处为了方便第5部分没有修改 //1.类似C方法定义 enum IpAddKind { V4, V6, } struct IpAddr { kind: IpAddKind, address: String, }; let i1 = IpAddr { kind: IpAddKind::V4, address: String::from("127.0.0.1"), }; let i2 = IpAddr { kind: IpAddKind::V6, address: String::from(&q ...
rust12.方法
12345678910111213141516171819202122232425262728293031323334#[derive(Debug)]struct Dog { name: String, weight: f32, height: f32, }impl Dog { fn get_name(&self) -> &str { &(self.name[..]) } fn get_height(&self) -> f32 { self.height } fn sound() { println!("wang-wang-wang!"); } }impl Dog { fn get_weight(&self) -> f32 { self.weight ...
rust11.结构体
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263fn main() { //1.定义结构体 struct User { name: String, count: String, nonce: u64, active: bool, } //2.创建结构体实例 let xiaoming = User { name: String::from("Xiaoming"), count: String::from("80001000"), nonce: 10000, active: true, }; //3.修改结构体字段 let mut xiaohuang ...
rust10.slice
字符串slice是String的一部分引用
字面值就是slice
其他类型的slice
123456789101112131415161718192021222324fn main(){ let s = String::from("Hello World"); let h = &s[0..5];//从0到5之前 //let h = &s[0..=4]; //let h = &s[..=4]; //let h = &s[..5] let w = &s[6..11]; let w = &s[6..=10]; let w = &s[..];//等于整个s println!("w= {}",w); let ss = String::from("你好"); let w1 = &s[0..1];//报错,这个是因为ch ...
rust9.引用
普通方法
12345678910111213141516171819202122fn main(){ let s1 = gives_ownership(); //将hello的值转给s1,s1仍然可以使用。 println!("s1 = {}",s1); let s2 = String::from("hello"); let s3 = takes_and_gives_back(s2); //因为s2已经转移至s3中,所以s2不能继续使用了 //println!("s2 = {}",s2); let mut s4 = String::from("Hello"); let s3 = takes_and_gives_back(s4); let s4 = takes_and_gives_back(s3); println!("s4 = {}",s4); ...