{%hackmd C-_zW0vxSXSSsOkJHknc8g %} # Rust 的奇幻旅程 <br> ## Something [TOC] ---- Made by FanRende (030Mortal#5525) <img src="https://i.imgur.com/Template.png" width="40%"> --- ## 切片型別 利用參考及借用的方式將集合中部分的元素取出 ```rust fn main() { let s = String::from("hello world"); let hello = &s[0..5]; // &s[..5] let world = &s[6..s.len()]; // &s[6..] println!("{}", hello); println!("{}", world); } ``` ---- Rust 中 `str` 是專門指 String 的切片型別 ```rust let s = String::from("hello world"); let ss = &s; // &String let hello = &s[..]; // &str ``` 這些都會是 &str ```rust let hello_world = "Hello, World!"; let hello_world: &str = "Hello, World!"; let hello_world: &'static str = "Hello, world!"; ``` ```rust let hello_world_Sting = String::from("Hello, World!"); let hello_world = &hello_world_Sting[..]; ``` ---- ### 要遵守不能同時有<br>可變與不可變參考的規則 ```rust fn main() { let mut s = String::from("hello world"); let hello = &s[0..5]; // &s[..5] let world = &s[6..s.len()]; // &s[6..] println!("{}", hello); println!("{}", world); let not_available = &mut s; println!("{}, {}", hello, not_available); } ``` --- ## Struct 在 Rust 中實現 OOP 的 class ```rust fn main() { struct Foo { // <- This is a struct x: i32, y: i32, } let foo = Foo { // <- This is an instance of the struct x: 1, y: 2 }; } ``` ---- Rust 中沒有繼承 但可以用 `trait` 實現 interface 的功能 ```rust! trait Animal { fn make_sound(&self); } ``` <div class="flex-container"> <div class="flex-content"> ```rust! struct Dog { name: String, } impl Animal for Dog { fn make_sound(&self) { println!("{}: woof!", self.name); } } ``` </div> <div class="flex-content"> ```rust! struct Cat { name: String, } impl Animal for Cat { fn make_sound(&self) { println!("{}: meow!", self.name); } } ``` </div> </div> ---- ```rust! fn main() { let dog = Dog { name: "Rex".to_string() }; let cat = Cat { name: "Felix".to_string() }; dog.make_sound(); cat.make_sound(); } ``` output: ``` Rex: woof! Felix: meow! ``` ---- ### struct update syntax ```rust struct Rectangle { width: u32, height: u32, color: String, id: u32, } let rect = Rectangle { width: 30, height: 50, color: String::from("blue"), id: 1, }; let rect2 = Rectangle { id: 2, ..rect }; ``` --- ## Enum 將類似的物件或描述枚舉出來 <div class="flex-container"> <div class="flex-content"> ```rust enum Color { Red, Green, Blue, } ``` ```rust struct Point { x: i32, y: i32, color: Color, } ``` </div> <div class="flex-content"> ```rust fn main() { let p = Point { x: 0, y: 0, color: Color::Red }; } ``` </div> </div> ---- 另一個範例 ```rust enum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), } impl Message { fn call(&self) { // 在此定義方法本體 println!("called!") } } let m = Message::Write(String::from("hello")); m.call(); ``` --- ### match - 用來比對數值 - 由多個 pattern arm 組成 - 當數值符合 pattern 就會執行 arm - 一個數值可以匹配多個 pattern 進而執行多個 arm - 順序會影響執行結果 - e.g. ```rust match color { Color::Red => println!("Red"), Color::Green => println!("Green"), Color::Blue => println!("Blue"), } ``` ---- ### if let ```rust let config_max = Some(3u8); match config_max { Some(max) => println!("最大值被設為 {}", max), _ => (), } ``` ```rust let config_max = Some(3u8); if let Some(max) = config_max { println!("最大值被設為 {}", max); } ``` --- 參考 / 延伸閱讀: - [學習 Rust](https://www.rust-lang.org/zh-TW/learn) - [The Rust Programming Language](https://doc.rust-lang.org/book/) - ChatGPT <div class="flex-container"> <div class="flex-content"> </div> <div class="flex-content"> </div> </div> <style> .gray { color: gray; font-size: 0.5em; } .slides .rust { font-size: 0.75em !important; line-height: 1.2em !important; } .mermaid { background-color: rgba(1, 1, 1, .2) !important; } .slides code { background-color: #444 !important; border-radius: 10px; white-space : pre-wrap !important; padding-right: 0.1em; padding-left: 0.1em; } .code-wrapper code { background-color: inherit !important; border-radius: inherit; } .flex-container { display: flex; justify-content: center; } .flex-content { flex-grow: 1; } </style> <style> /* Customize website's scrollbar like Mac OS */ ::-webkit-scrollbar { -webkit-appearance: none; width: 7px; } ::-webkit-scrollbar-thumb { border-radius: 4px; background-color: rgba(128, 128, 128, 1); -webkit-box-shadow: 0 0 1px rgba(255, 255, 255, .5); } </style>
{"metaMigratedAt":"2023-06-18T01:18:57.670Z","metaMigratedFrom":"YAML","title":"Rust 的奇幻旅程 Something","breaks":true,"description":"切片型別、Struct、Enum","slideOptions":"{\"theme\":\"dark\",\"transition\":\"fade\",\"previewLinks\":true}","contributors":"[{\"id\":\"82f6b599-31b8-4112-9dc5-7d7b7d6a3ebb\",\"add\":5487,\"del\":339}]"}
    188 views
   Owned this note