News for hackers. Stuff that matters.
Re:Rust for C++ programmers – Part 8: destructuring by tbrock on Sunday July 13, 2014 @03:16AM (#8026877)
Nick this is awesome, thanks! As someone who is just learning C++ for my day job this is a great resource and gateway into rust (and potentially fun side projects).

Just a gentle suggestion, have you considered putting some syntax highlighting on the code portions of your site so that it is more readable? I find it a little hard with the grey on grey scheme even though the Rust itself is super elegant.

Re:Rust for C++ programmers – Part 8: destructuring by aminbandali on Sunday July 13, 2014 @08:11AM (#8027175)
I had that feeling too, so I started publishing his posts in GitBook, which has nice formatting and syntax highlighting: https://www.gitbook.io/book/aminb/rust-for-c

The repo, from which the book is built, is available on GitHub: https://github.com/aminb/rust-for-c

I tried contacting and asking him before, but didn't hear back, so I just went ahead and did it myself.

Re:Rust for C++ programmers – Part 8: destructuring by doublec on Sunday July 13, 2014 @07:22PM (#8028879)
As a general rule, if you don't hear back don't assume permission. While you did this for helpful reasons an author of an article may still prefer not to have things republished so they can control updates, error correction and future republishing of the work. Gitbook does look great though, thanks for pointing it out.
Re:Rust for C++ programmers – Part 8: destructuring by tbrock on Sunday July 13, 2014 @09:36AM (#8027300)
Very cool, thank you!
Re:Rust for C++ programmers – Part 8: destructuring by aminbandali on Sunday July 13, 2014 @11:15AM (#8027540)
you're welcome ;)
Re:Rust for C++ programmers – Part 8: destructuring by zwieback on Sunday July 13, 2014 @09:03AM (#8027253)
Love it - bookmarked.
Re:Rust for C++ programmers – Part 8: destructuring by aminbandali on Sunday July 13, 2014 @11:17AM (#8027546)
Glad you liked it! GitBook is a pretty cool tool I'd heard about before; and when I started reading Nick's posts, I knew it was the best time to try it haha.
Re:Rust for C++ programmers – Part 8: destructuring by jackweirdy on Sunday July 13, 2014 @06:50AM (#8027081)
I've found Rust By Example to be a really great help with learning Rust.

http://rustbyexample.com/

Re:Rust for C++ programmers – Part 8: destructuring by shadowmint on Sunday July 13, 2014 @07:49AM (#8027156)
Be useful to document the distinction between:

   match x { (y) => { ... }
and:

   match x { (ref mut y) => { ... } 
In there too. I think ref x matches (particularly from Result) are pretty common.
Re:Rust for C++ programmers – Part 8: destructuring by kzrdude on Sunday July 13, 2014 @09:24AM (#8027284)
A restriction not mentioned -- you can't destructure a struct with destructor (impl Drop).
Re:Rust for C++ programmers – Part 8: destructuring by panzi on Sunday July 13, 2014 @09:26AM (#8027287)
Interesting. What's the rationale for that?
Re:Rust for C++ programmers – Part 8: destructuring by Jweb_Guru on Sunday July 13, 2014 @11:03AM (#8027506)
Note that this reply may have some factual errors since I'm still learning this aspect of Rust myself.

It's not that structs with Drop can't be destructured, but that they can't be destructured and moved. For why that's a bad idea, consider this example (from the original bug implementing this behavior, https://github.com/rust-lang/rust/issues/3147):

    let {f, g, h, _} = move foo
(The `move` keyword is no longer in Rust, but local variables with a type implementing Drop are implicitly moved, not shallow-copied, when used as an rvalue, since it is only safe to run a destructor once on the same structure).

The problem is that there's no obvious place to run the destructor. Destruction for items with the Drop trait in Rust proceeds as follows: first the value's destructor is run, then the value is completely destroyed, and then the values of any of its children are run. Clearly, then, the destructor must be run after the let, or the item's fields would be uninitialized at destruction time--but there's no longer any available reference to the item upon which Drop can be run.

There's at least one proposal to fix this floating out there (https://github.com/rust-lang/rust/issues/11855). The basic idea is to alter destruction semantics so that the three steps of destruction can be split up. This would allow Rust to run the destructor for the original item, then move any referenced fields out of the item, then destroy the item, and then run the field-level destructors for any items going out of scope.

Re:Rust for C++ programmers – Part 8: destructuring by chrismorgan on Sunday July 13, 2014 @10:28AM (#8027415)
It would necessarily require the destructor not to be run, which could be unsafe.