mutable

Javascript: Using reviver function, I seem can't get to alter all the keys, while concating the numbers

霸气de小男生 提交于 2019-11-29 16:49:27
I just want to change all the keys in batchesX. But I can't seem to alter all keys, because of concat. This is what I learned from post . Please advise how I can change all keys with numbers. var batchesX = '[{"batch":"0010002033"},{"batch":"0010001917"},{"batch":"0000020026"},{"batch":"0000017734"},'+ '{"batch":"0000015376"},{"batch":"0000014442"},{"batch":"0000014434"},{"batch":"0000014426"},'+ '{"batch":"0000013280"},{"batch":"0000012078"},{"batch":"0000012075"},{"batch":"0000012072"},'+ '{"batch":"0000011530"},{"batch":"0000011527"},{"batch":"0000011342"},{"batch":"0000010989"},'+ '{"batch

Two dimensional vectors in Rust

爷,独闯天下 提交于 2019-11-29 14:17:34
Editor's note: This question predates Rust 0.1 (tagged 2013-07-03) and is not syntactically valid Rust 1.0 code. Answers may still contain valuable information. Does anyone know how to create mutable two-dimensional vectors in Rust and pass them to function to be manipulated? This is what I tried so far: extern crate std; fn promeni(rec: &[u8]) { rec[0][1] = 0x01u8; } fn main() { let mut rec = ~[[0x00u8,0x00u8], [0x00u8,0x00u8] ]; io::println(u8::str(rec[0][1])); promeni(rec); io::println(u8::str(rec[0][1])); } You could use the macro vec! to create 2d vectors. fn test(vec: &mut Vec<Vec<char>>

How can I improve this design that forces me to declare a member function const and declare variables mutable?

不打扰是莪最后的温柔 提交于 2019-11-29 14:14:48
For some reason I am iterating over elements of a class in an std::set and would like to slightly modify the keys, knowing that the order will be unchanged. Iterators on std::set are const_iterators because if the key is modified, it might result in a bad order and therefore in set corruption. However I know for sure that my operations won't change the order of my elements in the set. For the moment, here is my solution: class Foo { public: Foo(int a, int b): a_(a),b_(b) {} ~Foo(){} bool operator < (const Foo& o) const { return this.a_ < o.a_ ; } void incrementB() const { ++b_; } // <-- the

Why doesn't the compiler report an error when a variable not declared as mutable is modified?

梦想与她 提交于 2019-11-29 13:30:33
I installed Rust 1.13 and tried: fn main() { let x: u32; x = 10; // no error? } When I compiled this file there's some warnings, but there's no error. As I'm not declaring x as mut , shouldn't x = 10; cause an error? What you have written is identical to: let x: u32 = 10; The compiler will not permit you to mutate it thereafter: let x: u32; x = 10; x = 0; // Error: re-assignment of immutable variable `x` Note that it is a compiler error if you try to use an uninitialized variable: let x: u32; println!("{}", x); // Error: use of possibly uninitialized variable: `x` This feature can be pretty

Mutability of string when string doesn't change in C#

和自甴很熟 提交于 2019-11-29 12:56:38
If the string operation doesn't change the value of string, will that end up in creation of a new instance? For example, string str = "foo"; str += ""; I know the difference between string and StringBuilder in C#. Shamseer K No, a new instance will be created only when the string operation changes the value in the string variable. It can be proved using the ObjectIDGenerator class. It's worth reading this complete article for proof. using System; using System.Text; using System.Runtime.Serialization; namespace StringVsStringBuilder { class Program { static void Main(string[] args) {

Mutable borrow in a loop

≡放荡痞女 提交于 2019-11-29 10:46:26
I have the following code: struct Baz { x: usize, y: usize, } struct Bar { baz: Baz, } impl Bar { fn get_baz_mut(&mut self) -> &mut Baz { &mut self.baz } } struct Foo { bar: Bar, } impl Foo { fn foo(&mut self) -> Option<&mut Baz> { for i in 0..4 { let baz = self.bar.get_baz_mut(); if baz.x == 0 { return Some(baz); } } None } } Rust Playground It fails to compile with: error[E0499]: cannot borrow `self.bar` as mutable more than once at a time --> src/main.rs:23:23 | 23 | let baz = self.bar.get_baz_mut(); | ^^^^^^^^ mutable borrow starts here in previous iteration of loop ... 29 | } | - mutable

Can I always convert mutable-only algorithms to single-assignment and still be efficient?

China☆狼群 提交于 2019-11-29 10:16:44
The Context The context of this question is that I want to play around with Gene Expression Programming (GEP), a form of evolutionary algorithm, using Erlang . GEP makes use of a string based DSL called ' Karva notation '. Karva notation is easily translated into expression parse trees, but the translation algorithm assumes an implementation having mutable objects: incomplete sub-expressions are created early-on the translation process and their own sub-expressions are filled-in later-on with values that were not known at the time they were created. The purpose of Karva notation is that it

How do I pass a reference to mutable data in Rust?

孤街浪徒 提交于 2019-11-29 09:31:27
I want to create a mutable struct on the stack and mutate it from helper functions. #[derive(Debug)] struct Game { score: u32, } fn addPoint(game: &mut Game) { game.score += 1; } fn main() { let mut game = Game { score: 0 }; println!("Initial game: {:?}", game); // This works: game.score += 1; // This gives a compile error: addPoint(&game); println!("Final game: {:?}", game); } Trying to compile this gives: error[E0308]: mismatched types --> src/main.rs:19:14 | 19 | addPoint(&game); | ^^^^^ types differ in mutability | = note: expected type `&mut Game` found type `&Game` What am I doing wrong?

Swift - How to mutate a struct object when iterating over it

筅森魡賤 提交于 2019-11-29 05:31:34
I am still not sure about the rules of struct copy or reference. I want to mutate a struct object while iterating on it from an array: For instance in this case I would like to change the background color but the compiler is yelling at me struct Options { var backgroundColor = UIColor.blackColor() } var arrayOfMyStruct = [MyStruct] ... for obj in arrayOfMyStruct { obj.backgroundColor = UIColor.redColor() // ! get an error } struct are value types, thus in the for loop you are dealing with a copy. Just as a test you might try this: Swift 3: struct Options { var backgroundColor = UIColor.black }

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