How to use struct self in member method closure

Split your data and methods into smaller components, then you can take disjoint borrows to various components on self: fn fetch_access_token(_base_url: &str) -> String { String::new() } fn get_env_url() -> String { String::new() } #[derive(Default)] struct BaseUrl(Option<String>); impl BaseUrl { fn get(&mut self) -> &str { self.0.get_or_insert_with(|| get_env_url()) } } #[derive(Default)] struct App { base_url: … Read more

How do I return a reference to something inside a RefCell without breaking encapsulation?

You can create a new struct similar to the Ref<‘a,T> guard returned by RefCell::borrow(), in order to wrap this Ref and avoid having it going out of scope, like this: use std::cell::Ref; struct FooGuard<‘a> { guard: Ref<‘a, MutableInterior>, } then, you can implement the Deref trait for it, so that it can be used as … Read more

tech