#Learning program design in Rust - Here's some of the important stuff

28 messages · Page 1 of 1 (latest)

foggy trench
#

would mostly just like some tips on designing & structuring projects

mode.rs

use ratatui::layout::Margin;

pub static MODE_MARGIN: Margin = Margin::new(3, 2);

pub trait Mode {
    fn enter(&mut self) -> anyhow::Result<()>;
    fn exit(&mut self) -> anyhow::Result<()>;
}

clicker.rs

use ratatui::{buffer::Buffer, layout::{Margin, Rect}, widgets::{Paragraph, Widget}};

use crate::modes::mode::{MODE_MARGIN, Mode};


#[derive(Debug)]
pub struct ClickerMode {
    pub toggled: bool, //overall toggled
    pub clicking: bool, //left mouse pressed
}

impl ClickerMode {

}

impl Mode for ClickerMode {
    fn enter(&mut self) -> anyhow::Result<()> {
        println!("enter clicker");   
        Ok(())
    }

    fn exit(&mut self) -> anyhow::Result<()> {
        println!("exit clicker");
        Ok(())
    }
}

impl Widget for &ClickerMode {
    fn render(self, area: Rect, buf: &mut Buffer) {
        Paragraph::new("CLICKER MODE").render(area.inner(MODE_MARGIN), buf);
    }
}

Recorder & trainer have a similar structure.

tranquil kiln
#

enable rust syntax highlighting in your code blocks

#

by writing them like

#

```rs
code here
```
or
```rust
code here
```

#

in general you can replace rust with the language it just has to be on the same line

foggy trench
#

okay, there

#

thanks

#

anyone who may stumble onto this, i am mainly trying to learn if im on the right track, how i can improve, maybe incorporate macros or something, more traits, idk

tranquil kiln
#

and uhh make it a const

#

there should be a way to make it const

foggy trench
tranquil kiln
foggy trench
#

okay

foggy trench
tranquil kiln
#

you want pub const

#

not static

#

its a numeric constant

#

this is 2 ints

#

it doesnt need to be static

foggy trench
#

okay, i just didnt know what u meant

#

yeah ur right

#

also, any tips on measuring performance in rust?

tranquil kiln
#

perf

#

gotta be on linux though for those

#

then theres also normal benching