Questions tagged [mutability]

Mutability is property of any function, variable or expression whose values are subject to changes with "side-effects". In other words, the value does not have referential transparency.

Overview

Mutability is property of any function, variable or expression where values are subject to changes with "side-effects". In other words, the value does not have referential transparency.

When the property is present on any component of a program, that program component is said to be "mutable".

Mutability may also generally refer to the question of whether or not a program component is mutable. When a program component is not "mutable" it is said to be "immutable".

See also

304 questions
174
votes
7 answers

Swift make method parameter mutable?

How can I deal with this error without creating additional variable? func reduceToZero(x:Int) -> Int { while (x != 0) { x = x-1 // ERROR: cannot assign to 'let' value 'x' } return x } I don't want to create…
Gabriel
  • 1,877
  • 2
  • 12
  • 8
134
votes
3 answers

How do I work around mutability in moment.js?

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…
Shengbo1618
  • 1,443
  • 2
  • 9
  • 5
133
votes
22 answers

Aren't Python strings immutable? Then why does a + " " + b work?

My understanding was that Python strings are immutable. I tried the following code: a = "Dog" b = "eats" c = "treats" print a, b, c # Dog eats treats print a + " " + b + " " + c # Dog eats treats print a # Dog a = a + " " + b + " " + c print a #…
jason
  • 5,391
  • 6
  • 23
  • 26
83
votes
2 answers

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

I have the following code: extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec = (0..10).collect(); let mut slice: &[u32] = vec.as_mut_slice(); thread_rng().shuffle(slice); } and get the following…
le_me
  • 3,089
  • 3
  • 26
  • 28
77
votes
3 answers

How to iterate through a Hashmap, print the key/value and remove the value in Rust?

This should be a trivial task in any language. This isn't working in Rust. use std::collections::HashMap; fn do_it(map: &mut HashMap) { for (key, value) in map { println!("{} / {}", key, value); …
adapt-dev
  • 1,608
  • 1
  • 19
  • 30
56
votes
5 answers

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

I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior…
Drew
  • 8,675
  • 6
  • 43
  • 41
47
votes
5 answers

Which types are mutable and immutable in the Google Go Language?

In Google Go, I read that Strings are immutable, ok but are int's? What about other types? As a slightly older programmer I prefer mutability even though I know the benefits of immutability, I prefer to live dangerously. Know what types are mutable…
Phil
  • 46,436
  • 33
  • 110
  • 175
33
votes
1 answer

In Rust, what's the difference between "shadowing" and "mutability"?

In Chapter 3 of the Rust Book, Variables and Mutability, we go through a couple iterations on this theme in order to demonstrate the default, immutable behavior of variables in Rust: fn main() { let x = 5; println!("The value of x is {}",…
d8aninja
  • 3,233
  • 4
  • 36
  • 60
28
votes
4 answers

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

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…
asmeurer
  • 86,894
  • 26
  • 169
  • 240
22
votes
2 answers

Mutably borrow one struct field while borrowing another in a closure

I have a struct containing two fields and I want to modify one field (mutable borrow) using another field (immutable borrow), but I get an error from the borrow checker. For instance, the following code: struct Struct { field1: Vec, …
antoyo
  • 11,097
  • 7
  • 51
  • 82
17
votes
2 answers

How to change the variable from inside Fn closure in Rust?

I have the following code (playground): struct A { pub vec: Vec, } impl A { fn perform_for_all(&mut self, f: F) { for mut i in &mut self.vec { f(i); } } } fn main() { let mut a = A…
VP.
  • 15,509
  • 17
  • 91
  • 161
17
votes
1 answer

cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable

First, let the code speak: #[derive(Debug)] struct Bar; #[derive(Debug)] struct Qux { baz: bool } #[derive(Debug)] struct Foo { bars: Vec, qux: Qux, } impl Foo { fn get_qux(&mut self) -> &mut Qux { &mut self.qux …
Felix Schlitter
  • 501
  • 4
  • 12
17
votes
1 answer

Scala: Mutable vs. Immutable Object Performance - OutOfMemoryError

I wanted to compare the performance characteristics of immutable.Map and mutable.Map in Scala for a similar operation (namely, merging many maps into a single one. See this question). I have what appear to be similar implementations for both…
Jeff
  • 14,831
  • 15
  • 49
  • 59
16
votes
1 answer

How can I make only certain struct fields mutable?

I have a struct: pub struct Test { pub x: i32, pub y: i32, } I'd like to have a function that mutates this — easy: pub fn mutateit(&mut self) { self.x += 1; } This makes the entire struct mutable for the duration of the function call…
user6467981
15
votes
2 answers

Where is a MutexGuard if I never assign it to a variable?

I don't understand "where" the MutexGuard in the inner block of code is. The mutex is locked and unwrapped, yielding a MutexGuard. Somehow this code manages to dereference that MutexGuard and then mutably borrow that object. Where did the MutexGuard…
AnimatedRNG
  • 1,859
  • 3
  • 26
  • 39
1
2 3
20 21