# Small Working Example of Rust's Stable Structures

**URL:** <https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379>\
**Category:** Developers\
**Tags:** rust, stable-memory\
**Created:** [August 25, 2023, 6:19pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379 "2023-08-25T18:19:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephgranata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/josephgranata/32/21930_2.png) [@josephgranata](https://forum.dfinity.org/u/josephgranata)\
**Post date:** [August 25, 2023, 6:19pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/1 "2023-08-25T18:19:52Z")

</div>

We are currently working on simulating a directory structure, with directories, file metadata, and content inside of them.

We started working with Motoko Hashmaps, and we have done some progress but I would really like to avoid the pre-upgrade and post-upgrade calls.

Could anyone share some working code of Rust Stable Structures, it would help us understand them better. Thanks!

P.S. This is the link to the IC’s Stable Structures in Rust:

> **[GitHub - dfinity/stable-structures: A collection of data structures for...](https://github.com/dfinity/stable-structures)**
>
> A collection of data structures for fearless canister upgrades. - GitHub - dfinity/stable-structures: A collection of data structures for fearless canister upgrades.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 25, 2023, 8:14pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/2 "2023-08-25T20:14:13Z")

</div>

Do you mean other examples than those already provided in the stable-structures repo?

[https://github.com/dfinity/stable-structures#more-examples](https://github.com/dfinity/stable-structures#more-examples)

[https://github.com/dfinity/stable-structures/tree/main/examples/src](https://github.com/dfinity/stable-structures/tree/main/examples/src)

---

<div class="post-metadata">

**Author:** ![josephgranata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/josephgranata/32/21930_2.png) [@josephgranata](https://forum.dfinity.org/u/josephgranata)\
**Post date:** [August 26, 2023, 5:10pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/3 "2023-08-26T17:10:31Z")

</div>

Indeed David, those examples are just a template, and a very small example.

Not a real small app of any kind, like for a example a TODO app or a small recipe app, something that actually would benefit from the stable data structures and has now the advantage of not needing the pre-upgrade and post-upgrade calls for data persistence.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 26, 2023, 5:46pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/4 "2023-08-26T17:46:51Z")

</div>

In that case you for example, maybe, have a look to [Internet Identity](https://github.com/dfinity/internet-identity/) and the [Bitcoin canisters](https://github.com/dfinity/ic/tree/master/rs/bitcoin) repo. They both use the stable structures. Hope that helps.

---

<div class="post-metadata">

**Author:** ![josephgranata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/josephgranata/32/21930_2.png) [@josephgranata](https://forum.dfinity.org/u/josephgranata)\
**Post date:** [August 26, 2023, 6:10pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/5 "2023-08-26T18:10:16Z")

</div>

Yes, that will help David, much appreciated.

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [September 26, 2023, 5:42pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/6 "2023-09-26T17:42:41Z")

</div>

There are also stable (in terms of using stable memory directly) [BTrees in Motoko](https://github.com/sardariuss/MotokoStableBTree). Not sure though you well tested/maintained this library is @sardariuss

---

<div class="post-metadata">

**Author:** ![sardariuss](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sardariuss/32/39258_2.png) [@sardariuss](https://forum.dfinity.org/u/sardariuss)\
**Post date:** [September 26, 2023, 6:22pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/7 "2023-09-26T18:22:46Z")

</div>

The library is currently getting a small rework, I will update soon the [post](https://forum.dfinity.org/t/completed-icdevs-org-bounty-24-stablebtree-mokoko-up-to-10k/14867) about it.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [September 27, 2023, 12:51am UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/8 "2023-09-27T00:51:31Z")

</div>

Also you might want to consider using the ic-wasi-polyfill and compiling to wasm32-wasi, if I’m not mistaken you get a file-system for free, either in memory or stable.

I wouldn’t consider implementing one from scratch.

---

<div class="post-metadata">

**Author:** ![josephgranata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/josephgranata/32/21930_2.png) [@josephgranata](https://forum.dfinity.org/u/josephgranata)\
**Post date:** [October 4, 2023, 11:53pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/9 "2023-10-04T23:53:13Z")

</div>

Jordan,

That is a mouth full of information, is there anywhere that I can research this topic? No idea now how to do the steps you mentioned.

Thanks!

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 5, 2023, 2:51am UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/10 "2023-10-05T02:51:14Z")

</div>

Implement Wasi: [Introducing WASI for IC](https://forum.dfinity.org/t/introducing-wasi-for-ic/18583)

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 5, 2023, 2:52am UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/11 "2023-10-05T02:52:01Z")

</div>

Then you should just have a file system, no need to implement one. It can be in memory on the heap or in stable memory

---

<div class="post-metadata">

**Author:** ![josephgranata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/josephgranata/32/21930_2.png) [@josephgranata](https://forum.dfinity.org/u/josephgranata)\
**Post date:** [October 6, 2023, 10:09pm UTC](https://forum.dfinity.org/t/small-working-example-of-rusts-stable-structures/22379/12 "2023-10-06T22:09:20Z")

</div>

Thanks Jordan I will read the forum post.
