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

Instead of creating a brand new type, you can use Ref::map (since Rust 1.8). This has the same result as Levans’ existing answer: use std::cell::Ref; impl Foo { pub fn get_items(&self) -> Ref<‘_, Vec<i32>> { Ref::map(self.interior.borrow(), |mi| &mi.vec) } } You can also use new features like impl Trait to hide the Ref from the … Read more

How do I create a Vec from a range and shuffle it?

Rand v0.6.0 The Rng::shuffle method is now deprecated; rand::seq::SliceRandom trait should be used. It provides the shuffle() method on all slices, which accepts an Rng instance: // Rust edition 2018 no longer needs extern crate use rand::thread_rng; use rand::seq::SliceRandom; fn main() { let mut vec: Vec<u32> = (0..10).collect(); vec.shuffle(&mut thread_rng()); println!(“{:?}”, vec); } See it … Read more

How do I work around mutability in moment.js?

There’s a Moment.js plugin on NPM called frozen-moment – You could use moment().freeze() in place of Object.freeze(moment()). Otherwise, vanilla Moment.js has a clone method that should help you avoid mutability problems, so you could do something like this: var a = moment(), b = a.clone(); // or moment(a) UPDATE: It has been two years since … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)