use std::cell::RefCell; struct Ctx { pub index: RefCell, msg: Vec, } impl Ctx { fn new() -> Self { Ctx { index: RefCell::new(0), msg: vec![] } } fn next(&self) { *self.index.borrow_mut() += 1; } } // func (c *Context) Next() { // c.index++ // for c.index < int8(len(c.handlers)) { // c.handlers[c.index](c) // c.index++ // } // } #[test] fn test_ref() { let c = Ctx::new(); c.next(); c.next(); c.next(); println!("index:{}", c.index.borrow()); }