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
14
votes
11 answers

returning IList vs Array in C#?

I was recently asking someone why he preferred to return a strongly-typed array over an IList. I had always thought that programming against an interface was the most flexible and best way program when faced with a project having a long life. So it…
mkelley33
  • 5,323
  • 10
  • 47
  • 71
14
votes
3 answers

Is there a way to make an immutable reference mutable?

I want to solve a leetcode question in Rust (Remove Nth Node From End of List). My solution uses two pointers to find the Node to remove: #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next:…
Aylei
  • 368
  • 2
  • 8
14
votes
4 answers

How to use struct self in member method closure

How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url(): fn fetch_access_token(_base_url: &String) -> String { String::new() } fn get_env_url() -> String { String::new() } pub…
Aqrun
  • 481
  • 1
  • 4
  • 10
14
votes
1 answer

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

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…
Nathan Fox
  • 582
  • 6
  • 16
12
votes
8 answers

How to keep an array with objects immutable in javascript?

I want to make an array based on two arrays - "ideaList" and "endorsements" declared globally. As ideaList and endorsements are used in other parts of the program I need them to be immutable, and I thought that .map and .filter would keep this…
Pierre
  • 219
  • 1
  • 4
  • 11
11
votes
7 answers

jQuery object are immutable?

Hello a new noob on jQuery here and I was wondering if jQuery objects are immutable. For example: var obj1 = $(""); var obj2 = obj1.append("something"); Will obj1 and obj2 be the same meaning obj2 will reference obj1? UPDATE: The above…
Daniel
  • 1,225
  • 2
  • 15
  • 31
11
votes
2 answers

When is it okay to modify a variable in functional languages?

So I'm teaching myself functional programming using Racket Scheme, and I love it so far. As an exercise for myself, I've been trying to implement a few simple tasks in a purely functional way. I know immutability is an important part of functional…
11
votes
1 answer

Const-correctness in C

Apparently it's good practice to use const unless something is meant to be mutable, but how far do you go? If I have an array of strings, should my function signature include this? char const * const * const my_strings I'm not going to be modifying…
mk12
  • 25,873
  • 32
  • 98
  • 137
10
votes
2 answers

How does interior mutability work for caching behavior?

I'm trying to create a struct that takes a Path and, on demand, loads the image from the path specified. Here's what I have so far: extern crate image; use std::cell::{RefCell}; use std::path::{Path}; use image::{DynamicImage}; pub struct…
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
10
votes
1 answer

How to implement a trait for any mutability?

Can mutability be a generic parameter in traits? I'd like to implement a trait for a mutable and an immutable variant of a type without having to copy&paste the impl block. trait Foo {…} impl Foo for *const T {…} impl Foo for *mut T…
Kornel
  • 97,764
  • 37
  • 219
  • 309
9
votes
1 answer

Immutable borrow tied to mutable borrow causes "cannot borrow `*self` as mutable more than once at a time"

I am learning Rust by exercise. In this file the goal is to update cells as in a spreadsheet: when a value changes, all cells that are derived from it have to be recalculated. Here, those are called that cell's parents. Updating a cell value proves…
Arne J
  • 415
  • 2
  • 9
9
votes
1 answer

How to interpret immutable references to mutable types in Rust?

It seems that I cannot mutate anything if there is any immutable reference in my chain of dereferencing. A sample: fn main() { let mut x = 42; let y: &mut i32 = &mut x; // first layer let z: &&mut i32 = &y; // second layer **z = 100;…
domin
  • 1,192
  • 1
  • 7
  • 28
9
votes
1 answer

Passing an immutable reference when a mutable reference exists

I have a for loop that iterates over a slice of Point structs. The Points will have some fields modified in the loop, so the function containing the loop requires a mutable reference to the slice. The problem arises when I need to pass a (immutable)…
tverghis
  • 957
  • 8
  • 31
9
votes
3 answers

How does the Rust compiler know `Cell` has internal mutability?

Consider the following code (Playground version): use std::cell::Cell; struct Foo(u32); #[derive(Clone, Copy)] struct FooRef<'a>(&'a Foo); // the body of these functions don't matter fn testa<'a>(x: &FooRef<'a>, y: &'a Foo) { x; } fn…
John
  • 1,856
  • 2
  • 22
  • 33
9
votes
1 answer

Cannot borrow as immutable - String and len()

let mut result = String::with_capacity(1000); result.push_str("things... "); result.push_str("stuff... "); result.truncate((result.len() - 4)); However, this is a compile error. Something to do with the borrow checker and possibly…
jocull
  • 20,008
  • 22
  • 105
  • 149
1
2
3
20 21