Skip to main content
Filter by
Sorted by
Tagged with
5 votes
1 answer
91 views

I'm trying to implement a binary search tree. The parent struct, BinSearchTree, has a pointer to a root node: pub struct BinSearchTree { root: Option<Box<Node>>, size: u32, } ...
workerjoe's user avatar
  • 2,765
0 votes
0 answers
28 views

When iterating over a hashmap, I want to conditionally remove some entries. Matching with mut map gives me mutable access to the hashmap stored in Wrapper. Attempting to loop over the entries with for ...
rust-nub's user avatar
0 votes
1 answer
51 views

I'm working with a struct Node and node_map defined as pub struct Node<'a> { id: i32, next: HashMap<String, &'a Node<'a>>, } let mut node_map = HashMap::<i32, ...
SPARTACUS5329's user avatar
0 votes
0 answers
92 views

I'd like advice on how numpy arrays and Python lists are passed into functions. Specifically, VBA and other languages I'm familiar with can be explicit in the function definition if an argument is ...
Murray Anderson's user avatar
0 votes
0 answers
79 views

This is my C# code: record struct recName(int A) { } And this is generated code behind the scenes: internal struct recName : IEquatable<recName> { public recName(int A) { <A&...
FR3Y4's user avatar
  • 1
0 votes
0 answers
55 views

I'm trying to figure out exactly why I get this compile error in this very simple code; cannot borrow p as mutable more than once at a time as I do not store p more than once mutably; either ...
spike's user avatar
  • 11
0 votes
2 answers
98 views

I'm learning Rust by reading the book and doing rustlings and watching videos; I think I understand what's happening here, but I don't understand what to do about it. #[derive(Debug)] enum Message { ...
Andrew Luhring's user avatar
1 vote
2 answers
81 views

I am trying to understand mutability. When I declare a variable in Rust with let mut a = String::from("Foo"); I can modify the Object, e.g.: a.push_str("Bar"); Or assign a new ...
Gerhard77's user avatar
1 vote
1 answer
76 views

I am attempting to create something similar to the code below. Is it safe to cast raw pointers as such given that I am only temporarily modifying the immutable struct (I undo the changes I make)? #[...
John Simon's user avatar
3 votes
1 answer
144 views

Minimal example of a game with a player which owns a position and walks as time passes. The following compiles: use std::thread::sleep; use std::time::Duration; struct Player { position: usize, } ...
guicostaarantes's user avatar
0 votes
1 answer
113 views

I'm having trouble understanding when to use mutable references as opposed to immutable references in traits. The following example for a Visitor implementation is taken from Rust Design Patterns: mod ...
Schottky's user avatar
  • 2,109
2 votes
1 answer
123 views

string a = "John"; string b = "Doe"; a = b; // Shallow copying strings b = "Elon Musk"; Console.WriteLine(a); // Output: Doe This prints "Doe", meaning the ...
Emre Bener's user avatar
  • 1,504
1 vote
1 answer
97 views

let mut a = Box::new("123".to_string()); let b = Box::new( &mut a); b.push('4'); assert_eq!( "1234", b.as_str()); // lets see the types: // let x001: Box<&...
Frank Schwidom's user avatar
0 votes
1 answer
165 views

In order to learn Rust I'm just writing trivial programs. Here I've written a LinkedList with an insert function that adds nodes to the end of the list. The function works as expected but I don't ...
Qwertyzw's user avatar
  • 532
1 vote
1 answer
290 views

I've tried everything from Rc to Arc and Mutex async_std which wouldn't even compile despite it apparently including wasm support. I've been fighting with this error for a few days now and I can't ...
Yaroslava Laurie's user avatar
1 vote
1 answer
152 views

Here is my code, it can compile trait AppendBar { fn append_bar(self) -> Self; } impl AppendBar for Vec<String> { fn append_bar(mut self) -> Self { self.push("Bar&...
dpr's user avatar
  • 359
1 vote
1 answer
75 views

I was trying to implement a Networking Module for my personal project and I was following TDD. When I am writing the test cases I came across a compile error and I was not able to map the error with ...
nath's user avatar
  • 2,848
0 votes
1 answer
64 views

I'm working on extending an interpreter written in Rust to support executing code in a new format. I have two structs—LanguageAContext and LanguageBContext— where the latter needs mutable access to ...
ian-colcanon's user avatar
0 votes
1 answer
84 views

I'm a newbuy in Rust. I need to store a collection of closures (fn_list) that can use variables from the context, after that I need to call these closures. Playground code: pub struct MyScope { ...
Nick Nizovtsev's user avatar
2 votes
2 answers
2k views

I still struggle to grasp why &mut self is required to modify the internal state of an object owned by my struct. I understand why I have to use at least &self, because I don't want to consume ...
try-catch-finally's user avatar
0 votes
0 answers
46 views

I am writing a small tictactoe game in Angular. I have a function checkForWin() which checks all the possible winning combinations for a win. (Horizontal, Vertical, and Diagonal) The function works ...
ThomasRones's user avatar
0 votes
0 answers
133 views

As i understand vals are immutable in Scala. However in case of inheritance we can override a val defined in the base class. If the val member already got created when the base class was constructed ...
Player's user avatar
  • 388
1 vote
2 answers
315 views

Consider the following Kotlin implementation of a string builder: class StringBuilder { private val items = mutableListOf<String>() fun append(item: String): StringBuilder { ...
Matthew Layton's user avatar
0 votes
2 answers
251 views

I've been playing around with collision detection, and I've been working on a function that collides two sets of objects with each other. To allow it to be run in parallel, only the first set is ...
Kenkron's user avatar
  • 573
1 vote
1 answer
82 views

i need to have a thread that recursively checks a variable while the main thread changes the variable. however, it appears that with move, the variable is not being changes by the lambda in register. (...
HyperCodec's user avatar
2 votes
2 answers
2k views

Polar's DataFrame does not provide a method to update the value of a single cell. Instead, we have to use the DataFrame.apply or DataFrame.apply_at_idx methods that update a whole column / Series. ...
Benjamin Du's user avatar
  • 2,001
0 votes
1 answer
86 views

I want to have two types (in my question-fitted examples I will mark them as IdHandler counting IDs, and IdUser using the IDs), where the dependent one of them should not outlive (hence, lifetimes) ...
Kirill Semyonkin's user avatar
0 votes
1 answer
73 views

I'm starting my first project in Rust. I try to make a Zwave crate. I'm stuck with the problem of ref sharing. For now, the code is divided in 3 structs: Network, which contains global Zwave network ...
Mathias DA COSTA's user avatar
0 votes
0 answers
79 views

I understand that the reason methods on strings return a new string is that strings are immutable, so the method can't modify the string you pass to it. With an array, if you pass it to a method, that ...
RNdev's user avatar
  • 1,291
0 votes
1 answer
173 views

I'm trying to remove an element from an array: pub fn update(arr: &mut Vec<Pipe>) { // move pipes for pipe in arr.iter_mut() { //first mutable borrow occurs here ...
paspielka's user avatar
2 votes
1 answer
667 views

I have a very basic problem in a rust relating to mutability of objects inside a vector. I have a need to get a mutable reference to an object from within a method of the struct as follows: struct ...
The amateur programmer's user avatar
0 votes
1 answer
119 views

According to this question, it is not possible to borrow parts of a struct, only the entire struct. This makes sense, however I would not expect the following code (playground) to compile, which it ...
frankplow's user avatar
  • 522
0 votes
2 answers
91 views

This is leetcode #37 (Sudoku solver). I have a question regarding modifying an input list in-place within a recursive function. The code below pretty much does the job as the print(board) does print ...
LarryC's user avatar
  • 3
-2 votes
1 answer
58 views

I was reading this very informative question and answer and learned about this behavior for the first time: calling def foo(l=[]): l.append(1) print(l) foo() foo() foo([]) foo() prints [1] [...
DoodleVib's user avatar
  • 156
0 votes
1 answer
287 views

In Kotlin, I'm trying to have a mutable generic value in sealed class A, and a mutable generic Number value in sealed class B with a mutable Long/... value in final C/...; but whenever I change A and ...
M. Elbrecht's user avatar
4 votes
1 answer
720 views

Sorry if this is a dumb question, I'm relatively new with Rust and just can't crack this double mutable borrowing error. I'm trying to create an AVL tree method that finds an appropriate position into ...
Skelectric's user avatar
0 votes
2 answers
115 views

I can't make this code work, I understand I need to change the self reference to a mutable reference but this will just spawn a new set of errors. struct Context { values: Vec<u32>, } trait ...
solomancode's user avatar
0 votes
3 answers
373 views

Let's say I have this code: let mut s = "hi".to_string(); let c = || s.push_str(" yo"); c(); It doesn't compile and generates this error: error[E0596]: cannot borrow `c` as ...
Fajela Tajkiya's user avatar
3 votes
1 answer
746 views

I wrote a working solution to the third proposed exercise in section 8.3 of The Book, but some of the behavior defies my intuition. Specifically, it appears that I'm able to mutate a vector that ...
HandsomeGorilla's user avatar
2 votes
1 answer
35 views

I looked in the js docs and while studying the doc it mentioned in object.assign() If the source value is a reference to an object, it only copies the reference value. In my below snippet, one alters ...
Bilal Malik's user avatar
0 votes
1 answer
37 views

I'm using the prae crate for validation and the following function gives me errors: fn advance_rotors(&mut self) { self.rotors.get()[0].rotate(); let mut iterhandle = self.rotors.iter_mut()....
Brandon Piña's user avatar
0 votes
4 answers
246 views

Consider the two following codes: import numpy as np mainlist = [np.array([0,0,0, 1]), np.array([0,0,0,1])] for i in range(len(mainlist)): mainlist[i] = mainlist[i][0:2] print(mainlist) # [...
Vincent's user avatar
  • 61.1k
0 votes
2 answers
283 views

i have a struct Screen with its implementation pub struct Screen { stdin: Stdin, // types are irrelevant stdout: MouseStdout, } impl Screen { // ... pub fn handle_keys_loop<F: ...
alexzander's user avatar
  • 1,935
0 votes
0 answers
14 views

Here is my code: list1 = ["dog", "cat"] list2 = list1 list2.append ("fish") print(list1) print(list2) # both list1 and list2 will be [“dog”, “cat”, “fish”]. I do ...
Betty's user avatar
  • 19
0 votes
1 answer
545 views

I was going through some third party code and I ran onto this snippet for going through an array. Since this is a respectful code base I'm wandering what is the secret behind the trouble of moving ...
Chupacabra's user avatar
1 vote
1 answer
73 views

I am trying to add a new property to an object which is part of an array of objects. The main array looks like this: 0: {key: 'Fruits', props: Array(22)} 1: {key: 'Dried', props: Array(1)} 2: {key: '...
marincean adrian's user avatar
3 votes
1 answer
267 views

I'm new to Haskell, so please forgive if this question is dumb. Imagine that we have two data structures bound to the names x and y. x is mutable. y is not. As a matter or principle, does x ...
James Strieter's user avatar
0 votes
1 answer
1k views

I'm trying to read the raw contents of a binary file, so they can be manipulated in memory. As far as I understand, bytes() objects are immutable, while bytearray() objects are mutable, so I read the ...
CugelTC's user avatar
0 votes
3 answers
181 views

Here is the question: You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions. Here is my code: def ...
Ethan Kho's user avatar
0 votes
1 answer
81 views

From what I understand, Python is a pass by object reference language, which means that if the original value is mutable, every shallow copy will be affected (and vice versa). So, something like: x = [...
articuno's user avatar

1
2 3 4 5
7