Last active
June 17, 2022 16:07
-
-
Save kriogenia/0ea0d7126ea6e48f7f9e9b09b0606abc to your computer and use it in GitHub Desktop.
Starting point of Dynamic tuple indices in Rust
This file contains 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 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