This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Ch28 練習:綜合專案概念複習 | |
| /// | |
| /// 將每個 todo!() 替換為正確的程式碼,讓程式通過編譯並印出正確結果。 | |
| /// 執行方式:cargo run -p ch28_capstone_project --example exercise_1 | |
| // === 練習 1 用到的 trait === | |
| use std::fmt; | |
| // === 練習 4 用到的 trait === | |
| use std::io; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use axum::extract::{Path, Query, State}; | |
| use axum::routing::{delete, get, post}; | |
| use axum::{Json, Router}; | |
| use serde::Deserialize; | |
| use std::sync::Arc; | |
| use crate::error::AppError; | |
| use crate::model::{Bookmark, CreateBookmarkRequest}; | |
| use crate::service::BookmarkService; | |
| use crate::storage::JsonStore; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Ch27 練習:Rust 開發實務 | |
| /// | |
| /// 將每個 todo!() 替換為正確的程式碼,讓程式通過編譯並印出正確結果。 | |
| /// 執行方式:cargo run -p ch27_development_practices --example exercise_1 | |
| // === 練習 1:為函式加上 rustdoc 註解 === | |
| // 在下方函式上方加上 /// 文件註解,包含: | |
| // - 函式說明 | |
| // - # Examples 區塊(含可執行的範例) | |
| // |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //! # 開發實務工具箱 | |
| //! | |
| //! 本模組示範 Rust 文件註解(rustdoc)的最佳實務, | |
| //! 包含函式文件、範例、以及 doc test。 | |
| /// 計算兩個整數的最大公因數(GCD)。 | |
| /// | |
| /// 使用歐幾里得演算法,時間複雜度為 O(log(min(a, b)))。 | |
| /// | |
| /// # Arguments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Ch26 練習:設計模式與慣用寫法 | |
| /// | |
| /// 將每個 todo!() 替換為正確的程式碼,讓程式通過編譯並印出正確結果。 | |
| /// 執行方式:cargo run -p ch26_design_patterns --example exercise_1 | |
| // ============================================================ | |
| // 練習 1:Builder Pattern(ServerConfig builder) | |
| // ============================================================ | |
| // 實作一個 ServerConfig 的 builder,支援鏈式呼叫 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ============================================================ | |
| // Ch26:設計模式與慣用寫法 | |
| // ============================================================ | |
| // 展示 Rust 中常見的設計模式:Builder、Typestate、RAII、型別驅動 API | |
| // 執行方式:cargo run -p ch26_design_patterns | |
| use std::fmt; | |
| // ============================================================ | |
| // 1. Builder Pattern(建造者模式) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Ch25 練習:Cargo 進階與生態系 | |
| /// | |
| /// 將每個 todo!() 替換為正確的程式碼,讓程式通過編譯並印出正確結果。 | |
| /// 執行方式:cargo run -p ch25_cargo_and_ecosystem --example exercise_1 | |
| fn main() { | |
| // === 練習 1(基礎):cfg! 巨集和條件編譯 === | |
| // 使用 cfg! 巨集判斷目前是否為 debug 模式 | |
| // cfg!(debug_assertions) 在 debug 模式下為 true | |
| // 提示:直接使用 cfg!(debug_assertions) 作為布林值 | |
| let is_debug: bool = todo!("使用 cfg! 巨集判斷是否為 debug 模式"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // === Feature Flag 與條件編譯 === | |
| // 使用 cfg attribute 根據條件選擇不同的實作 | |
| // 實際專案中,feature flag 定義在 Cargo.toml 的 [features] 區段 | |
| // 這裡用 cfg(debug_assertions) 和 cfg(target_os) 來示範相同的概念 | |
| /// 根據編譯模式回傳不同的日誌等級 | |
| fn log_level() -> &'static str { | |
| // debug_assertions 在 `cargo run` (debug) 時為 true | |
| // 在 `cargo run --release` 時為 false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Ch24 練習:Web API 伺服器 | |
| /// | |
| /// 將每個 todo!() 替換為正確的程式碼,讓所有 assert 通過。 | |
| /// 這些練習不需要啟動伺服器,專注於資料模型和業務邏輯。 | |
| /// 執行方式:cargo run -p ch24_web_api_server --example exercise_1 | |
| use serde::{Deserialize, Serialize}; | |
| // === 練習 1:定義 Todo 結構體 === | |
| // 定義一個 Todo 結構體,包含以下欄位: | |
| // - id: u64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 執行:cargo run -p ch24_web_api_server,然後用 curl 測試 | |
| // | |
| // 範例 curl 指令: | |
| // curl http://localhost:3000/todos | |
| // curl -X POST http://localhost:3000/todos -H "Content-Type: application/json" -d '{"title":"學 Rust"}' | |
| // curl http://localhost:3000/todos/1 | |
| // curl -X DELETE http://localhost:3000/todos/1 | |
| use axum::{ | |
| Json, Router, |
NewerOlder