3 ms·
> a weak reference inside a refcell, It'd likely be the other way, that is, you'd put a RefCell inside of an Rc/Weak. > it doesn't look like refcell works wel
by steveklabnik 9y ago
> a weak reference inside a refcell,
It'd likely be the other way, that is, you'd put a RefCell inside of an Rc/Weak.
> it doesn't look like refcell works well with traits
It should; I bet you had problems since you did it the other way around.
> I'm a bit frustrated because this is so easy in C.
You could do it the same way as you do it in C, if you're willing to resort to `unsafe`.
FWIW, Rust sort of changes the equation for what's easy here; thread-safe concurrency? Simple! Data structures? Hard! C is the other way around. So feeling a bit frustrated is normal; your C skills won't carry over, but it feels like they should.
> I am imagining a special way to construct cyclical structures where everything inside would have the same lifetime and be destructed at once
This sounds like an arena to me, which is another option, for sure.
If you post to https://users.rust-lang.org/ https://users.rust-lang.org/, someone might be willing to whip up an example, or if you post your in-progress stuff, someone might be able to fix it for you.
https://crates.io/crates/petgraph https://crates.io/crates/petgraph might also just already have what you need, maybe.
- jeffdavis 9y agoThank you. I will look into these and give it another try. It's a good point that maybe I should just have the right expectations here, and expect data structures to be hard in rust. I looked around a bit and it looks like these thing are quite challenging in haskell as well.
- lederhosen 9y agoIn Haskell it is easy, you can not create cyclic data structures ;-)
- tome 9y agoHuh? Sure you can.
- wtetzner 9y agoCyclic data structures can be created in Haskell, in large part due to laziness.
- lederhosen 9y agoIf you see data structures as cyclic, it is not "in large part due to laziness", but _only_ due to laziness. If you look at the memory representation I think (but I am not sure) there will be no cycles between allocated objects. Only a thunk. Using strict data types, I think you agree that cycles can not be created. Non strict data structures I agree can be seen as cyclic, but I prefer seeing them as infinite.
- lomnakkus 9y agoCounterexample: $ ghci GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help Prelude> let ones = 1 : ones Prelude> take 50 ones [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] You can take as many as you like. (The 'ones' list contains a tail which links back to its head, producing an infinite list.) Obviously, this is a trivial example, you can do much more interesting things with mutually recursive bindings.
- tene 9y agolet cycle = "cycle " ++ cycle