mutability Questions

2

Solved

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 str...
Monograph asked 11/12, 2017 at 7:20

3

Solved

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::{P...
Wavelength asked 18/8, 2015 at 1:46

5

Solved

I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, /...

3

Solved

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...
Superjacent asked 17/1, 2019 at 14:3

6

I was just going through mutable and immutable structures in python. It was written that "Strings are immutable" in Python i.e We cannot alter them Consider the code: str1='Rohit' str1.replace('R'...
Edgington asked 14/11, 2017 at 14:0

4

Solved

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...
Nagari asked 10/2, 2018 at 6:11

1

Every now and then, I run into the same issue with borrowing (or not borrowing) mutable variables inside a loop, and I finally sat down and compiled a minimal example. As a result, the code is a li...
Woald asked 4/12, 2021 at 18:33

8

Solved

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...
Cenotaph asked 25/3, 2019 at 8:4

3

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<String, String>) { for (key, value) in map { prin...
Suppressive asked 16/8, 2017 at 23:38

3

Solved

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 Ob...
Hardan asked 22/6, 2015 at 11:45

1

Solved

The Rust book talks about having multiple readers and multiple mutable references to an object as a data race situation that may lead to issues. For example, this code: fn main() { let mut...
Ober asked 17/5, 2020 at 12:38

22

Solved

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 trea...
Omarr asked 1/2, 2012 at 14:56

1

Solved

I'm attempting to chain Iterators: var it = Iterator(1) it.next it = Iterator(2) ++ it it.next it.hasNext This infinitely loops on the hasNext as you can see here: https://scastie.scala-lang.org...
Dibucaine asked 15/5, 2020 at 22:30

4

Solved

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 see...
Fidel asked 25/10, 2012 at 5:55

7

Solved

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 ...
Recruit asked 6/6, 2014 at 8:57

1

Solved

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...
Riva asked 21/5, 2019 at 6:47

1

Solved

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 l...
Clarkson asked 6/5, 2019 at 12:21

2

Solved

I came from C# world, and used to arrays being reference types. As I understand, in swift arrays are value types, but they try to play as reference ones. I don't actually know how to ask what I ne...
Readymade asked 18/1, 2016 at 21:19

1

Solved

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. ...
Stevenage asked 20/12, 2018 at 15:29

1

Solved

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() { ...
Kind asked 10/11, 2018 at 1:45

2

Solved

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 ...
All asked 14/7, 2018 at 5:50

3

I don't understand these cases: content = {'a': {'v': 1}, 'b': {'v': 2}} d1 = {'k1': {}} d2 = {'k2': {}} d1['k1'].update(content) print(d1) content['a']['v'] = 3 content['b']['v'] = 4 d2['k2...
Longhand asked 21/5, 2018 at 9:21

2

Solved

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 followin...
Dotti asked 2/4, 2016 at 22:2

1

Solved

I have following Groovy script: mymap = ['key': 'value'] println mymap v = mymap.get('notexistkey', 'default') println v println mymap When I run it I get following console output: [key:value...
Sprint asked 15/2, 2018 at 11:46

3

In this question about the passing of arguments in JavaScript functions, we learn that everything is passed by value in JavaScript. In Mozilla documents, it is mentioned that the primitive types ...
Kagoshima asked 29/7, 2017 at 10:54

© 2022 - 2025 — McMap. All rights reserved.