---
title: "Discussion on \"Self Referential Structs in Rust (Part 1)\" | Hashnode"
description: "Discussion on \"Self Referential Structs in Rust (Part 1)\". TL;DR\n\nUse Pin<Box<T>> for the held value (here, Me).\nMark Me as unpinnable by setting a field as std::marker::PhantomPinned.\nEg, _pinned: std::marker::PhantomPinned.\n\nUse Rc<RefCell<H>> for holder, (here, Holder) and store Me as raw\npointers.\nEg, me..."
url: "https://hashnode.com/posts/self-referential-structs-in-rust/61837eaefd5d634d016966d6"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Self Referential Structs in Rust (Part 1)](https://arunanshub.hashnode.dev/self-referential-structs-in-rust?utm_source=hashnode&utm_medium=feed)

TL;DR Use Pin<Box<T>> for the held value (here, Me). Mark Me as unpinnable by setting a field as std::marker::PhantomPinned. Eg, \_pinned: std::marker::PhantomPinned. Use Rc<RefCell<H>> for holder, (here, Holder) and store Me as raw pointers. Eg, me...

Nov 4, 2021 · 13 min read · arunanshub.hashnode.dev  
Updated Nov 28, 2021  
Read the article

-   [#rust](https://hashnode.com/tag/rust)
-   [#programming-blogs](https://hashnode.com/tag/programming-blogs)
-   [#linux](https://hashnode.com/tag/linux)
-   [#tutorial](https://hashnode.com/tag/tutorial)

## Written by

[Arunanshu Biswas](https://hashnode.com/@arunanshub)

@arunanshub

  
[View profile](https://hashnode.com/@arunanshub)

Cryptography and Machine Learning enthusiast and paranoid about performance without sacrificing readability.

## Responses 1

Markdown supported · ⌘↵ to post

[Oscar Ablinger](https://hashnode.com/@OscarAblinger)

[Aug 27, 2024](https://hashnode.com/posts/self-referential-structs-in-rust/61837eaefd5d634d016966d6/comment/66cdec8b67b8ffe5784d2bb8)

Good post! Made me understand pinning ^^

I only wish rust had a way to force immutability on a boxed value together with a lifetime. I mean something like this:

```
struct URI<'a> {
  raw: String<'a>,
  pub protocol: &'a str,
}
```

Assuming `protocol` points to a substring of `raw`, my understanding is, that this would be entirely unproblematic unless someone mutates `raw`. Since it's not public and none of the implemented methods for it, mutate it, is there any example of where this could lead to a dangling reference?

And with the lifetime on `String`, rust could ensure that, while `raw` may still be changed, the previous string would still have to be available for at least `'a`, ensuring that it won't break `protocol`.

I'm never sure whether I should be happy that rust "thinks of everything" and won't let me write potentially problematic code, or whether I just want to tell rust "trust me bro", because it doesn't apply to the actual code…
