mutable

Simple as possible example of returning a mutable reference from your own iterator

十年热恋 提交于 2019-12-01 21:56:20
This question is related, however it moreso covers the reason why the compiler cannot infer a safe lifetime when returning a mutable reference from Iterator::next , which I think I understand. My question is: What are the specific steps you can take when designing your own iterator so that it can produce mutable references? Ultimately, I'm hoping for a concise-as-possible, step-by-step, commented example of both an Iterator and its next implementation that I (and anyone) can go to as a clear reference when they run into this situation. unsafe examples are fine, I imagine they are probably

cannot move out of borrowed content when unwrapping a member variable in a &mut self method

依然范特西╮ 提交于 2019-12-01 03:09:05
问题 I was trying to make a Disjoint-Set data structure in Rust. The relevant code is: pub struct Set<'a, T: 'a> { rank: u32, value: T, parent: Option<&'a mut Set<'a, T>>, } impl<'a, T> Set<'a, T> { pub fn find(&'a mut self) -> &'a mut Set<'a, T> { match self.parent { None => self, Some(mut p) => { self.parent = Some(p.find()); self.parent.unwrap() } } } } The errors I get are: error[E0507]: cannot move out of borrowed content --> src/main.rs:9:15 | 9 | match self.parent { | ^^^^ cannot move out

pre-defined constants for non-trivial data types

不问归期 提交于 2019-12-01 00:16:23
My Goal: Create a C# class for predefined errors that have both an ID and a Message. Here was what I tried: public class MyError { public static readonly MyError OK = new MyError(0, "OK"); public static readonly MyError Bad = new MyError(1, "Bad Stuff"); public MyError(int id, string message) { this.Id = id; this.Message = message; } public readonly int Id; public readonly string Message; } This compiles just fine and I am sure it would work just fine in practice. But I always like to follow Code Analysis guidelines. In the above case, CA2104 is violated "Do not declare read only mutable

Why are System.Windows.Point & System.Windows.Vector mutable?

感情迁移 提交于 2019-11-30 11:17:37
Given that mutable structs are generally regarded as evil (e.g., Why are mutable structs “evil”? ), are there potential benefits that might have prompted the designers of the .NET framework to make System.Windows.Point & System.Windows.Vector mutable? I'd like to understand this so I can decide whether it would make sense to make my own similar structs mutable (if ever). It's possible the decision to make Point and Vector mutable was just an error in judgment, but if there was a good reason (e.g., a performance benefit), I'd like to understand what it was. I know that I've stumbled over the

Two dimensional vectors in Rust

大兔子大兔子 提交于 2019-11-30 09:15:45
问题 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:

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

隐身守侯 提交于 2019-11-30 08:57:51
问题 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); | ^^^^^

are user defined classes mutable

♀尐吖头ヾ 提交于 2019-11-30 08:54:12
问题 Say I want to create a class for car , tractor and boat . All these classes have an instance of engine and I want to keep track of all the engines in a single list. If I understand correctly if the motor object is mutable i can store it as an attribute of car and also the same instance in a list. I cant track down any solid info on whether user defined classes are mutable and if there is a choice to choose when you define them, can anybody shed some light? 回答1: User classes are considered

In Kotlin, how do you modify the contents of a list while iterating

冷暖自知 提交于 2019-11-30 01:29:27
I have a list: val someList = listOf(1, 20, 10, 55, 30, 22, 11, 0, 99) And I want to iterate it while modifying some of the values. I know I can do it with map but that makes a copy of the list. val copyOfList = someList.map { if (it <= 20) it + 20 else it } How do I do this without a copy? Note: this question is intentionally written and answered by the author ( Self-Answered Questions ), so that the idiomatic answers to commonly asked Kotlin topics are present in SO. Also to clarify some really old answers written for alphas of Kotlin that are not accurate for current-day Kotlin. First, not

How to make any view to draw to canvas?

陌路散爱 提交于 2019-11-29 19:44:23
I have a short question: Suppose I have a (mutable) bitmap that I need to modify (add images, texts, etc...) . Instead of messing around with many special classes for drawing to the canvas (paint, canvas, matrices and so on), I was thinking why not use the built in classes of Android for this task and only if i need really customized operations i could still use the canvas ? So, for example, in order to show any kind of view (that has no parent, of course) on the bitmap, I could call the next function : public void drawViewToBitmap(Bitmap b, View v, Rect rect) { Canvas c = new Canvas(b); // <=

Why are System.Windows.Point & System.Windows.Vector mutable?

限于喜欢 提交于 2019-11-29 16:59:36
问题 Given that mutable structs are generally regarded as evil (e.g., Why are mutable structs “evil”?), are there potential benefits that might have prompted the designers of the .NET framework to make System.Windows.Point & System.Windows.Vector mutable? I'd like to understand this so I can decide whether it would make sense to make my own similar structs mutable (if ever). It's possible the decision to make Point and Vector mutable was just an error in judgment, but if there was a good reason (e