Editing
Rust
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Using Box's== Using boxes is simple and allows for generics to be avoided. But it requires dynamic dispatch adding overhead. When Boxing a member variable in a struct, the struct's constructor must be updated to take a box and any setter methods. Also the at the constructor call site must now Box it's value. Users/consumers/owners of the struct however are unaffected. extern crate rand; use rand::{Rng, StdRng}; pub struct Context { rng: Box<Rng>, // <- This is changed } impl Context { pub fn new(rng: Box<Rng>) -> Context { // <- Now this line must be changed Context {rng: rng} } pub fn set_rng(&self, rng: Box<Rng>) { // <- ...this line must be changed self.rng = rng; } } impl SomeTrait for Context { //...Blah } fn use_context(ctx: &Context) { //... } fn main() { let seed: &[_] = &[1, 2, 3, 4]; let mut rng = StdRng::from_seed(seed); let mut ctx = Context::new(Box::new(rng)); // <- ...this line must be changed use_context(&ctx) }
Summary:
Please note that all contributions to Hegemon Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Hegemon Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information