mutability

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

情到浓时终转凉″ 提交于 2019-12-01 15:39:37
I have the following code: extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec<u32> = (0..10).collect(); let mut slice: &[u32] = vec.as_mut_slice(); thread_rng().shuffle(slice); } and get the following error: error[E0308]: mismatched types --> src/main.rs:9:26 | 9 | thread_rng().shuffle(slice); | ^^^^^ types differ in mutability | = note: expected type `&mut [_]` found type `&[u32]` I think I understand that the content of vectors and slices is immutable and that causes the error here, but I'm unsure. The signature of as_mut_slice is pub fn as_mut_slice<'a>(&'a mut

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

给你一囗甜甜゛ 提交于 2019-12-01 13:42:28
问题 I have the following code: extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec<u32> = (0..10).collect(); let mut slice: &[u32] = vec.as_mut_slice(); thread_rng().shuffle(slice); } and get the following error: error[E0308]: mismatched types --> src/main.rs:9:26 | 9 | thread_rng().shuffle(slice); | ^^^^^ types differ in mutability | = note: expected type `&mut [_]` found type `&[u32]` I think I understand that the content of vectors and slices is immutable and that

Immutable views of mutable types

半城伤御伤魂 提交于 2019-11-30 20:13:38
I have a project where I need to construct a fair amount of configuration data before I can execute a process. During the configuration stage, it's very convenient to have the data as mutable. However, once configuration has been completed, I'd like to pass an immutable view of that data to the functional process, as that process will rely on configuration immutability for many of its computations (for instance, the ability to pre-compute things based on initial configuration.) I've come up with a possible solution using interfaces to expose a read-only view, but I'd like to know if anybody

globals() vs locals() mutability

孤人 提交于 2019-11-30 10:19:21
In Python, globals() returns a representation of the global symbol table, while locals() returns a representation of the local state. While both return a dictionary, changes to globals() are effected in the global symbol table, while change to locals() have no effect. Why is this the case? Function locals are highly optimised and determined at compile time, CPython builds on not being able to alter the known locals dynamically at runtime. You can see this when decoding a function bytecode: >>> import dis >>> def foo(): ... a = 'bar' ... return a + 'baz' ... >>> dis.dis(foo) 2 0 LOAD_CONST 1 (

Immutable views of mutable types

醉酒当歌 提交于 2019-11-30 04:33:13
问题 I have a project where I need to construct a fair amount of configuration data before I can execute a process. During the configuration stage, it's very convenient to have the data as mutable. However, once configuration has been completed, I'd like to pass an immutable view of that data to the functional process, as that process will rely on configuration immutability for many of its computations (for instance, the ability to pre-compute things based on initial configuration.) I've come up

How do I work around mutability in moment.js?

╄→гoц情女王★ 提交于 2019-11-29 21:59:29
I've run into a problem where I have to store the initial values of a moment object but I'm having some trouble preventing my variable from changing along with the original object. Unfortunately Object.freeze() doesn't work, because moment.js returns an "Invalid date" error when I try to format that. 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

globals() vs locals() mutability

南笙酒味 提交于 2019-11-29 15:25:44
问题 In Python, globals() returns a representation of the global symbol table, while locals() returns a representation of the local state. While both return a dictionary, changes to globals() are effected in the global symbol table, while change to locals() have no effect. Why is this the case? 回答1: Function locals are highly optimised and determined at compile time, CPython builds on not being able to alter the known locals dynamically at runtime. You can see this when decoding a function

How can I modify self in a closure called from a member function?

假如想象 提交于 2019-11-29 13:46:18
I am trying to calculate legal chess moves and am having problems satisfying the borrow checker. I have a struct Chess that implements these methods (non-important code replaced by ... ): // internal iterator over (possibly not legal) moves fn get_moves<F>(&self, func: F) where F: Fn(/* ... */), { func(/* ... */); // move 1 func(/* ... */); // move 2 func(/* ... */); // etc... } fn is_legal_move(&mut self) -> bool { // notice this takes a mutable self. For performance // reasons, the move is made, legality is checked, then I // undo the move, so it must be mutable to be able to move pieces

Is making in-place operations return the object a bad idea?

我是研究僧i 提交于 2019-11-29 05:30:19
I'm talking mostly about Python here, but I suppose this probably holds for most languages. If I have a mutable object, is it a bad idea to make an in-place operation also return the object? It seems like most examples just modify the object and return None . For example, list.sort . Andrew Gorcester Yes, it is a bad idea. The reason is that if in-place and non-in-place operations have apparently identical output, then programmers will frequently mix up in-place operations and non-in-place operations ( List.sort() vs. sorted() ) and that results in hard-to-detect errors. In-place operations

How do I work around mutability in moment.js?

て烟熏妆下的殇ゞ 提交于 2019-11-28 17:17:38
问题 I've run into a problem where I have to store the initial values of a moment object but I'm having some trouble preventing my variable from changing along with the original object. Unfortunately Object.freeze() doesn't work, because moment.js returns an "Invalid date" error when I try to format that. 回答1: 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