前言
以前谁是魔我杀谁,现在我杀谁谁就是魔。——《拔魔》
\;\\\;\\\;
目录
- 前言
- Rust基本知识
- 结构体
- 元组+结构体
- 局部作用域
- 冻结
- 字面量
- Vec元素的类型由第一个push进的类型确定
- type别名
- from和into
- TryFrom和TryInto
- 表达式
Rust基本知识
结构体
#[derive(Debug)]
struct Window_table{
width : i32,
height : i32,
position : (i32,i32),
id : i32,
name :String,
}
fn main(){
let W=1920;
let H=1080;
let a = Window_table{
width : 960,
height : 600,
position : (W/2-480,H/2-300),
id : 0x0000_0000_0000_00ff, //255
name : String::from("new win")
};
println!("{:#?}",a);
}
元组+结构体
struct Color(u8,u8,u8); //3个字节
struct Point(f64,f64); //16个字节
fn main(){
let a=Color(166,70,70);
println!("a=({},{},{})",a.0,a.1,a.2);
let a=Point(400.0,300.0);
println!("a=({},{})",a.0,a.1);
}
局部作用域
目前这个例子还是和其他的语言一样,没什么特殊的。
fn main() {
let a = 1;
{
let b = 2;
println!("b={}", b);
let a = 5_f32;
println!("a={}", a);
}
println!("a={}", a);
let a = 'a'; //shadowing
println!("a={}", a);
}
冻结
fn main() {
let mut a = 7i32;
{
let a = a;
//a = 50; //报错,因为外部可变的a被内部不可变的a冻结了!
}
a = 3;
}
字面量
对于没有表明类型的整数,编译器默认是i32;对于没有表明类型的浮点数,编译器默认是f64
fn main() {
let x = 117u8;
let y = 20u32;
let z = 3.1f32;
let i = 5;
let f = 3.1415;
println!("size of `x` in bytes: {}", std::mem::size_of_val(&x));
println!("size of `y` in bytes: {}", std::mem::size_of_val(&y));
println!("size of `z` in bytes: {}", std::mem::size_of_val(&z));
println!("size of `i` in bytes: {}", std::mem::size_of_val(&i));
println!("size of `f` in bytes: {}", std::mem::size_of_val(&f));
}
Vec元素的类型由第一个push进的类型确定
fn main() {
let a = 5u8;
let b=String::from("Hello");
let c:u8 = 1;
let mut v = Vec::new();
v.push(a);
//v.push(b); //错误,因为类型被确定成u8了
v.push(c);
println!("{:?}", v);
}
type别名
//别名用type
type T1 = u64;
type T2 = u64;
type u8_ = u64;
fn main() {
let a: T1 = 5 as u8_;
let b: T2 = 2 as u8_;
println!("{} a + {} b = {} unit?", a, b, a + b);
}
from和into
from函数就是把传入类型转换成自定义的类型,通过是把传入数据赋值给自定义类型的属性
use std::convert::From;
#[derive(Debug)]
struct Number {
value: i32,
}
impl From<i32> for Number {
fn from(item: i32) -> Self {
Number { value: item }
}
}
fn main() {
let num = Number::from(30);
println!("num={:?}", num);
let a = 5;
let b: Number = a.into();
println!("b={:?}", b);
}
TryFrom和TryInto
和from、into一样,但是带错误处理
use std::convert::TryFrom;
use std::convert::TryInto;
#[derive(Debug, PartialEq)]
struct EvenNumber(i32);
impl TryFrom<i32> for EvenNumber {
type Error = ();
fn try_from(value: i32) -> Result<Self, Self::Error> {
if value % 2 == 0 {
Ok(EvenNumber(value)) //传入偶数就返回自定义类型
} else {
Err(()) //传入奇数就报错
}
}
}
fn main() {
assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8))); //ok=ok
assert_eq!(EvenNumber::try_from(5), Err(())); //err=err
let a: Result<EvenNumber, ()> = 8i32.try_into(); //和a.into()的用法一样
assert_eq!(a, Err(())); //ok!=err
let a: Result<EvenNumber, ()> = 5i32.try_into();
assert_eq!(a, Err(())); //err=err
}
\;\\\;\\\;
表达式
fn main() {
let x = 5u32;
let y = {
let a = x * x;
let b = a * x;
//返回值不需要分号,意味着最后一句的结果默认是外面接收的
b + a + x
};
let z = {
2 * x;
};
println!("x is {:?}", x);
println!("y is {:?}", y);
println!("z is {:?}", z);
}
表达式最后一句加了分号就表示没有返回值