What lifetimes do I use to create Rust structs that reference each other cyclically?

谁都会走 提交于 2019-11-27 01:58:07
Chris Morgan

It is not possible to create cyclic structures with borrowed pointers.

There is not any good way of achieving cyclic data structures at present; the only real solutions are:

  1. Use reference counting with Rc<T> with a cyclic structure with Rc::new and Rc:downgrade. Read the rc module documentation and be careful to not create cyclic structures that use strong references, as these will cause memory leaks.
  2. Use raw/unsafe pointers (*T).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!