Rust by Example(chapter15)

Derive(派生) 在Rust中,提供了各种各样的派生特性。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #[derive(PartialOrd, PartialEq)] struct Centimeters(f64); struct Inches(i32); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } fn main() { let foot = Inches(12); let meter = Centimeters(100.0); let cmp = if foot.to_centimeters() < meter { "smaller" } else { "bigger" }; println!("{}", cmp); } 在这里,首先定义了Centimeters,同时重载了比……

阅读全文

Rust by Example(chapter14)

Generics(泛型) Generics是Rust中的重要内容,能够最大化地减少代码的重复。fn foo<T>(arg: T) { ... }来定义一个函数,传入的参数为T。 1 2 3 4 5 6 7 8 9 10 11 12 struct Val { val: f64, } impl Val { pub fn value(&self) -> &f64 { &self.val } } fn main() { let x = Val { val: 3. }; let y = x.value(); } trait(接口) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16……

阅读全文

Rust by Example(chapter9-13)

模块 & crate & Cargo & Attributes 可见性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 mod my_mod { // 不可见 fn private_function() { println!("called `my_mod::private_function()`"); } // 可见 pub fn function() { println!("called `my_mod::function()`"); } // 可以嵌套 pub mod nested { pub fn function() { println!("called `my_mod::nested::function()`"); } } } mod my { // struct默认是私有的 pub struct OpenBox<T> { pub contents: T, } #[allow(dead_code)] pub struct ClosedBox<T> { contents: T, } impl<T> ClosedBox<T> { pub fn new(contents: T) -> ClosedBox<T> { ClosedBox { contents: contents, } } } } 包可……

阅读全文

Rust by Example(chapter8)

函数 函数定义 1 2 3 fn fun(v:i32) -> i32{ v + 3 } 定义类型以及返回类型,同时在Rust函数是一个表达式,也就是不需要return。 方法 1 2 3 4 5 6 7 struct Pair(Box<i32>, Box<i32>); impl Pair { fn destory(self) { let Pair(x,y) = self; println!("Destroying Pair({}, {})", x, y); } } 闭包(也称之为lambda表达式) 1 2 3 fn function(i: i32) -> i32 { i + 1 } let closure_annotated = |i: i32| -> i32 { i + 1 }; let one = || 1; 下面看一下指针……

阅读全文

Rust by Example(chapter7)

Rust是一个现代的系统级编程语言,侧重于安全、速度和并发。 条件控制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 fn main() { let n = 5; if n < 0 { print!("{} is negative", n); } else if n > 0 { print!("{} is positive", n); } else { print!("{} is zero", n); } let big_n = if n < 10 && n > -10 { println!(", and is a small number, increase ten-fold"); 10 * n } else { println!(", and is a big number, halve the number"); n / 2 }; println!("{} -> {}", n, big_n); } loop循环……

阅读全文

Rust by Example(chapter6)

Conversion Rust的核心就是traits,可以重载。 From and To 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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(123); println!("{:#?}", num); let x = 5; let num: Number = x.into(); println!("{:#?}", num); } 实现了From这个traits,就能够同时使用from以及into,用来从类型之间的转换。 1……

阅读全文

Rust by Example(chapter4-5)

变量绑定 简单的例子 1 2 3 4 //如果不使用会有警告 let noisy_unused_variable = 2u32; //前面添加了下划线就不会有警告 let _noisy_unused_variable = 2u32; Mutability 如果申请一个变量可以变化的话,那么就需要 1 let mut mutable_binding = 1; 变量作用域与变量名掩盖(Scope and Shadowing) 1 2 3 4 5 6 7 8 fn main() { let a = 3; { let a = 4; println!("{}", a); } println!("{}", a); } cast(强制转换) 1 2 3 4 5 6 7 8 9 10……

阅读全文

Rust by Example(chapter3)

Custom Types Rust中有两种自定义类型 struct enum struct 在结构体中,有三种struct的类型,分别为 元祖数据结构 类C struct unit struts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #[derive(Debug)] struct Person<'a> { // 'a 表示生命周期,当Person生命结束了,那么name也结束了。 name: &'a str, age: u8, } struct Nil; struct Pair(i32, f32);……

阅读全文

Rust by Example(chapter1)

Rust是一个现代的系统级编程语言,侧重于安全、速度和并发。 安装 1 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh hello world 1 2 3 fn main() { println!("Hello World!"); } 其中,println!是一个宏,功能是将字符串打印到终端。 编译方式如下 1 2 rustc hello.rs ./hello 或者使用 1 cargo run cargo是一个rust生态管理工具,我们在接下来的各种实例中都采用这种方式来运行……

阅读全文