mazes-for-programmers | Coding along with Mazes for Programmers '' by Jamis Buck
kandi X-RAY | mazes-for-programmers Summary
kandi X-RAY | mazes-for-programmers Summary
Coding along with "Mazes for Programmers" by Jamis Buck
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of mazes-for-programmers
mazes-for-programmers Key Features
mazes-for-programmers Examples and Code Snippets
Community Discussions
Trending Discussions on mazes-for-programmers
QUESTION
I'm going through Mazes for Programmers and thought I'd try to do it in Rust (the original code is in Ruby).
One of the first things you do in the book is to implement a Cell
class that includes links to other cells. Naively, I thought I'd be able to do something like this:
ANSWER
Answered 2017-Jan-05 at 22:27How can I create a struct with a HashMap where the keys the same type as the container
the original code is in Ruby
I'm not that well acquainted with Ruby, but I know a lot of similar languages so I'll make some educated guesses.
Ruby is a thoroughly garbage collected language. What it means is that you never get to store the keys of some type in a Ruby HashMap. You might think that you're storing a key of type Cell
, but what really happens is that you're storing a garbage-collected pointer to an instance of that Cell
.
And a pointer is just an index into some memory region.
Now, Rust is a different kind of language. In Rust you can store the actual Cell
right there in the underlying keys array of the HashMap. In other words, Rust gives you more control, and that often translates to better memory usage and speed.
But with control comes responsibility (unless you want to break things and break them fast). In Rust you are supposed to explicitly specify who owns the instance. And when you create a struct that points to itself (a cyclic graph), the ownership gets muddled.
So one way to implement a cyclic structure is to move the ownership concern out of the equation!
In Ruby the garbage collector solves this. Memory management is the concern of the garbage collector and you implement cyclic structures without ever dealing with it.
In Rust you can do the same by using an (experimental) garbage collector, maybe rust-gc.
Or you can get your hands dirty and actually manage the memory.
This isn't as hard as it sounds. All you need is some non-cyclic structure own the Cell. Vec
would suffice. After you have stored the Cell
in a vector, you don't have to worry about the ownership anymore. Because the Cell
is now owned by that vector, plain and simple. You can make cyclic structurs, referencing the Cell
by its vector index, just like the Ruby does with its pointers!
There are all kinds of variations on this. When you manage the ownership, you are the boss, the choice is yours. You can optimize some algorithms with memory pools, also known as arenas. You can use reference counting (there's no need for a vector then, but you'd want to carefully clear the references when removing the cells from your structure - in order to break the reference counting cycles). You can use a kind of deque to allocate the memory in chunks but without reallocations that vector does, then store direct pointers into that deque. Some of the options are mentioned in this reddit discussion.
But the principle is simple. Make sure that something (Vec
, Rc
, Gc
) covers the ownership concern. And when that's covered, you can program just like in Ruby, because the ownership is no longer an issue.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install mazes-for-programmers
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page