LoginSignup
2
1

More than 5 years have passed since last update.

Rc<RefCell<T>>で変更可能なポインタ

Last updated at Posted at 2018-11-04
use  std::rc::Rc;
use  std::cell::RefCell;

fn main() {
    let c = Rc::new(RefCell::new("aaa"));
    let c2 = c.clone();
    *c.borrow_mut() = "bbb";
    println!("{}", c2.borrow())
}

こう定義すればすべての参照がなくなってから破棄されるmutableな値を定義できる
*(*c)のあたりが忘れそうなのでメモ そんなことする必要がなかった!!
ちなみに  thread_local! を使いつつ変更可能な値を外に出したい時とかに使う(そのためにRcでラップした値が必要になったので調べてた)


追記.
@Pctg-x8 さんにコメント頂いたので修正させて頂きました 🙇

2
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1