News for hackers. Stuff that matters.
Re:Announcing Rust 1.20 by msbarnett on Thursday August 31, 2017 @04:04PM (#15143427)
> I am imagining a special way to construct cyclical structures where everything inside would have the same lifetime and be destructed at once.

The simple way to do that would be to allocate an array, and use indices into the array rather than pointers/references.

Doing it with pointers isn't so much harder in Rust than C as it is that Rust is making you deal with how hard it is to get this right, whereas in C the compiler is happy to let you think it's easy while you accidentally shoot yourself in the foot.

If you want the Rust compiler to accept your mistakes, you can always wrap it in an unsafe block ;)

Re:Announcing Rust 1.20 by jeffdavis on Thursday August 31, 2017 @05:13PM (#15143982)
But in my example this is not hard to get right in C. The tree is constructed (on the stack would be fine), then used for a while without mutating it, then freed all at once.

The thing that makes this hard in rust is destructors. If there's a cycle between A and B, and you destruct A first, then B, then B's destructor would see a dangling reference to A. And vice versa if you destruct B first.

But I don't need destructors, or at least ones that can see these references, so it's frustrating.

Re:Announcing Rust 1.20 by msbarnett on Thursday August 31, 2017 @05:54PM (#15144289)
> But in my example this is not hard to get right in C. The tree is constructed (on the stack would be fine), then used for a while without mutating it, then freed all at once.

It's still "hard to get right" in that at any time nothing is stopping you from violating any of the assumptions that make this "easy". It's never easy to write a solution that's "guaranteed to be safe" in C, but that's what you're trying to do by writing such a solution in Rust. To give but one example, nothing in C will stop your nodes from containing some resources which needs to be manually destructed and which get leaked when the stack frame is reclaimed.

Rust is going to require you to make those assumptions explicit, so that it can enforce them -- in this case, you need to explicitly restrict your solution to dealing with Copy types, which by construction don't implement Drop, and therefore have no destructors.

But at the end of the day, if all you want to do is swear to the compiler that you know what you're doing and you promise to not be stupid, wrap it in an unsafe and get C-style consequences if you got things wrong. You get C-Style easiness only by explicitly abandoning the attempt at guaranteeing safety for every type and scenario your solution could be used with.

Re:Announcing Rust 1.20 by whyever on Thursday August 31, 2017 @06:24PM (#15144493)
Nothing stops you from leaking memory in Rust either, it is considered memory safe, see std::mem::forget. Rust only safes you from use-after-free and double-free.
Re:Announcing Rust 1.20 by staticassertion on Thursday August 31, 2017 @06:58PM (#15144698)
And integer out of bounds, and (most) segfaults, and data races, etc
Re:Announcing Rust 1.20 by steveklabnik on Thursday August 31, 2017 @07:04PM (#15144737)
Notably integer overflow is NOT UB and is checked in debug builds.
Re:Announcing Rust 1.20 by staticassertion on Thursday August 31, 2017 @07:35PM (#15144924)
Sorry, I should have said array OOB.
Re:Announcing Rust 1.20 by steveklabnik on Thursday August 31, 2017 @07:44PM (#15144977)
Yeah, it's all good!
Re:Announcing Rust 1.20 by steveklabnik on Thursday August 31, 2017 @05:24PM (#15144075)
> But I don't need destructors, or at least ones that can see these references, so it's frustrating.

If you bound it so that it only accepts Copy types, then you can know there are no destructors.

Re:Announcing Rust 1.20 by dbaupp on Thursday August 31, 2017 @07:20PM (#15144838)
The tree itself will need destructors to clean up allocations, and that's what's problematic here: if the tree is destroyed top down, the destructor of the children may access the parent which has already been invalidated, and similarly destroying bottom up risks the destructor of the parent accessing the children.
Re:Announcing Rust 1.20 by jeffdavis on Thursday August 31, 2017 @06:08PM (#15144372)
Interesting. Then rust should not care about the destruction order, right?
Re:Announcing Rust 1.20 by steveklabnik on Thursday August 31, 2017 @06:53PM (#15144668)
For this purpose, no, I don't think. That said I might be missing something and maybe th compiler doesn't understand this, so I'm not saying it's a panacea in your case, just might be worth looking at.
Re:Announcing Rust 1.20 by burntsushi on Thursday August 31, 2017 @05:21PM (#15144039)
Since you want to use the C method, have you tried using raw pointers?
Re:Announcing Rust 1.20 by Animats on Thursday August 31, 2017 @07:06PM (#15144751)
The simple way to do that would be to allocate an array, and use indices into the array rather than pointers/references.

I've done that in code for a collision detection engine. The object descriptions for convex polyhedra have lists of faces, lists of edges, and lists of vertices, all referencing each other. The original implementation (I-Collide) actually used lists for them. When I re-implemented that in C++, I used arrays with indices for each of those. When you're done with a polyhedron, all those structures, which are owned by the Polyhedron object, go away more or less simultaneously.

Re:Announcing Rust 1.20 by fish_fan on Thursday August 31, 2017 @04:24PM (#15143611)
While it may be simpler, it also removes any safety that rust adds. I'd imagine you'd get more mileage out of just using pointers and unsafe blocks liberally to get lifetime checking where you can.
Re:Announcing Rust 1.20 by skariel1 on Thursday August 31, 2017 @05:01PM (#15143880)
It is still safer than c, as it will not allow any segfaults. The only thing that can happen is a runtime error but no memory corruption so it is more akin to say Java or Go
Re:Announcing Rust 1.20 by steveklabnik on Thursday August 31, 2017 @05:08PM (#15143941)
The context of this thread is unsafe, which can segfault.
Re:Announcing Rust 1.20 by ComputerGuru on Friday September 01, 2017 @11:09AM (#15148995)
You are describing the behavior of standard, safe rust code.

Using unsafe blocks throws all that away.