Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 23, 2019 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/1bb54653ce521bfc7f116ce0201bf9fe to your computer and use it in GitHub Desktop.
Save rust-play/1bb54653ce521bfc7f116ce0201bf9fe to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![allow(unused)]
#[derive(Debug)]
struct TextEngine {
id: u32,
}
#[derive(Debug)]
struct Ui {
engine: TextEngine,
labels: Vec<Label>,
}
#[derive(Debug)]
struct Label {
id: String,
text: String,
}
impl Ui {
fn new(e: TextEngine) -> Self {
let mut v: Vec<Label> = Vec::new();
v.push(Label::new("op", "clear"));
v.push(Label::new("mode", "over"));
v.push(Label::new("tool", "pastel"));
v.push(Label::new("tip", "4X"));
Ui {
engine: e,
labels: v,
}
}
fn get_label(&mut self, _id: &str) -> Option<&mut Label> {
for l in self.labels.iter_mut() {
if l.id == _id {
return Some(l);
}
}
None
}
fn set_mode(&mut self, mode: &str) {
let res = self.get_label("mode");
match res {
Some(label) => {
label.set_text(mode);
}
None => {
println!("ERROR");
}
}
}
}
impl Label {
fn new(id: &str, text: &str) -> Label {
Label {
id: String::from(id),
text: String::from(text),
}
}
fn text(&self) -> &str {
&self.text
}
fn set_text(&mut self, _text: &str) {
self.text = String::from(_text);
}
}
fn main() {
let e = TextEngine { id:12 };
let mut ui = Ui::new(e);
ui.set_mode("under");
println!("{:?}", ui);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment