sharing_state/
sharing_state.rs

1use setting_tracker::Setting;
2use std::rc::Rc;
3
4#[derive(Default, Debug)]
5struct Settings {
6    str: Setting<String>,
7}
8
9fn main() {
10    let mut settings = Settings::default();
11    let s = Rc::new("abc".to_string());
12    {
13        let mut ss = s.to_string();
14        settings.str.cb(move |o, n| {
15            println!("{:?} -> {:?}", o, n);
16            ss.push_str("...");
17            println!("{:?} from closure", ss);
18        });
19    }
20    println!("{}", s);
21    settings.str.set("new".to_string());
22    println!("{:?}", settings);
23}
24
25/*
26Output:
27    abc
28    "" -> "new"
29    "abc..." from closure
30    Settings { str: "new" }
31 */