extern crate pistonwindow; use pistonwindow::*; enum Page { Home, Settings, } fn main()…
- User
extern crate piston_window;
use piston_window::*;
enum Page { Home, Settings, }
fn main() { let mut window: PistonWindow = WindowSettings::new("Piston 簡單頁面切換", [640, 480]) .exit_on_esc(true) .build() .unwrap();
let mut current_page = Page::Home; while let Some(event) = window.next() { // 輸入事件:按鍵切換頁面 if let Some(Button::Keyboard(key)) = event.press_args() { current_page = match current_page { Page::Home => { if key == Key::S { Page::Settings } else { Page::Home } } Page::Settings => { if key == Key::H { Page::Home } else { Page::Settings } } } } window.draw_2d(&event, |c, g, _| { clear([1.0; 4], g); // 清白底 match current_page { Page::Home => { // 顯示 Home 頁面內容 text::Text::new_color([0.0, 0.0, 0.0, 1.0], 32) .draw( "Home Page: Press S for Settings", &mut Glyphs::new( "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", window.create_texture_context(), TextureSettings::new(), ).unwrap(), &DrawState::default(), c.transform.trans(50.0, 200.0), g, ).unwrap(); } Page::Settings => { // 顯示 Settings 頁面內容 text::Text::new_color([0.0, 0.0, 0.0, 1.0], 32) .draw( "Settings Page: Press H for Home", &mut Glyphs::new( "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", window.create_texture_context(), TextureSettings::new(), ).unwrap(), &DrawState::default(), c.transform.trans(50.0, 200.0), g, ).unwrap(); } } }); }} 請修正錯誤
- Assistant
- User
69 | pub fn new( | ^^^ help: consider removing the borrow | 21 - let mut glyphs = Glyphs::new(font_path, &mut texture_context, TextureSettings::new()).unwrap(); 21 + let mut glyphs = Glyphs::new(font_path, texture_context, TextureSettings::new()).unwrap(); |
For more information about this error, try
rustc --explain E0308. - Assistant
- User
完整程式碼
- Assistant
以下是完整且可運作的範例程式碼,使用 piston_window 文字繪製並做頁面切換。
假設你使用的Glyphs::new是接受擁有權(非 ),並在 draw 結束呼叫 。 - User
- Assistant
- User
warning: unused variable:
button--> src\main.rs:8:25 | 8 | fn input(&mut self, button: Button, state_manager: &mut StateManager) {} | ^^^^^^ help: if this is intentional, prefix it with an underscore:_button| = note:#[warn(unused_variables)]on by defaultwarning: unused variable:
state_manager--> src\main.rs:8:41 | 8 | fn input(&mut self, button: Button, state_manager: &mut StateManager) {} | ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore:_state_managererror[E0499]: cannot borrow
*self.current_stateas mutable more than once at a time --> src\main.rs:117:9 | 117 | self.current_state.input(button, self); | ^^^^^^^^^^^^^^^^^^^-----^^^^^^^^^----^ | | | | | | | first mutable borrow occurs here
| | first borrow later used by call | second mutable borrow occurs here - Assistant
- User
完整程式碼
- Assistant
- User
- Assistant
- User
error[E0599]: no method named
lenfound for fn itemfn([f32; 4], u32, ..., ..., [[f64; 3]; 2], ...) -> ... {text::<_, _>}in the current scope --> src\main.rs:119:31 | 119 | let width_estimate = text.len() as f64 * 24.0 / 2.0; | ^^^ method not found infn([f32; 4], u32, ..., ..., [[f64; 3]; 2], ...) -> ... {text::<_, _>}error[E0308]: mismatched types --> src\main.rs:124:19 | 124 | text_obj.draw(text, glyphs, &graphics::DrawState::default(), transf... | ---- ^^^^ expected
&str, found fn item | | | arguments to this method are incorrect | = note: expected reference&strfound fn itemfn([f32; 4], u32, ..., ..., [[f64; 3]; 2], ...) -> ... {text::<_, _>}note: method defined here --> C:\Users\fer52.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\piston2d-graphics-0.44.0\src\text.rs:66:12 | 66 | pub fn draw<C, G>( | ^^^^ = note: the full name for the type has been written to 'C:\Users\fer52\mynewcargo\my_project\target\debug\deps\my_project.long-type-4625740128220201285.txt' = note: consider using to print the full type name to the console error[E0499]: cannot borrow as mutable more than once at a time --> src\main.rs:43:31 | 42 | if let Some(top) = self.stack.last_mut() { | ---------- first mutable borrow occurs here 43 | top.input(button, self); | ----- ^^^^ second mutable borrow occurs here | | | first borrow later used by call - Assistant
- User
error[E0277]:
(dyn State + 'static)doesn't implementDebug--> src\main.rs:18:10 | 16 | #[derive(Debug)] | ----- in this derive macro expansion 17 | enum StateCommand { 18 | Push(Box), | ^^^^^^^^^^^^^^(dyn State + 'static)cannot be formatted using{:?}because it doesn't implementDebug| = help: the traitDebugis not implemented for(dyn State + 'static)
= help: the following other types implement traitDebug: dyn Any + Send + Sync dyn Any + Send dyn AnyFor more information about this error, try
rustc --explain E0277. - Assistant
你遇到的錯誤:
error[E0277]: `(dyn State + 'static)` doesn't implement `Debug`原因是你在
#[derive(Debug)]的enum StateCommand裡含有Box<dyn State>,但dyn State型別並沒有實作Debug,因此derive(Debug)會失敗。
解決方式
1. 取消對
StateCommand推導Debug你這個 enum 裡面包了 trait 物件,所以無法直接 derive
Debug。問你需不需要
StateCommand實作Debug?- 如果不需要調試輸出(println!("{:?}", ..))可以直接移除
#[derive(Debug)]。
2. (進階) 如果需要 debug,你必須要讓裡面
dyn State實作Debug,或包裝其他可 debug 的資料。但一般狀態機中這種 enum 不常直接 debug,建議移除
derive(Debug)。
修改建議
// 移除 derive(Debug),改成普通 enum enum StateCommand { Push(Box<dyn State>), Pop, None, }
這樣就能編譯成功。
補充說明
若你想在 debug 時能印出
StateCommand,可自己手動實作 ,例如: - 如果不需要調試輸出(println!("{:?}", ..))可以直接移除