Skip to content

Instantly share code, notes, and snippets.

@kriogenia
Last active June 17, 2022 16:07
Show Gist options
  • Save kriogenia/0ea0d7126ea6e48f7f9e9b09b0606abc to your computer and use it in GitHub Desktop.
Save kriogenia/0ea0d7126ea6e48f7f9e9b09b0606abc to your computer and use it in GitHub Desktop.
Starting point of Dynamic tuple indices in Rust
use std::{rc::Rc, cell::RefCell};
struct XLimit {
vector: Rc<RefCell<(i32, i32)>>,
limit: i32
}
impl XLimit {
fn limit(&self) {
if (*self.vector).borrow().0 > self.limit {
self.vector.borrow_mut().0 -= self.limit;
}
}
}
fn main() {
let vecref = Rc::new(RefCell::new((0, 100)));
let limit_x = XLimit { vector: vecref.clone(), limit: 250 };
for _ in 0..5 {
vecref.borrow_mut().0 += 100;
limit_x.limit();
println!("Position: {:?}", vecref);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment