# Rust: using stable storage as main storage - Example/Pattern

**URL:** <https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154>\
**Category:** Developers\
**Created:** [July 5, 2022, 10:21am UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154 "2022-07-05T10:21:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saikatdas0790](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/saikatdas0790/32/17147_2.png) [@saikatdas0790](https://forum.dfinity.org/u/saikatdas0790)\
**Post date:** [July 5, 2022, 10:21am UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/1 "2022-07-05T10:21:47Z")

</div>

I’ve read the article here by @roman-kashitsyn

> **[Effective Rust canisters](https://mmapped.blog/posts/01-effective-rust-canisters.html)**
>
> A compilation of useful patterns for developing IC canisters in Rust.

and the Community Conversation here:

[![](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/6/6dc438b74a6c9c2e62a7fcab51e3db98a0f92d96.jpeg "Community Conversations | Best Practices for Canisters in Rust") ](https://www.youtube.com/watch?v=36L33S_DYHY)

where he advises this [here](https://mmapped.blog/posts/01-effective-rust-canisters.html#stable-memory-main):

> Consider using stable memory as your main storage.

But I’ve been unable to find a simple example that goes over doing this and breaks down the steps with explanations.

I’ve also tried to follow along to the code for the Internet Identity canister here that is supposedly doing this but unable to grok it entirely:

> <https://github.com/dfinity/internet-identity/blob/main/src/internet_identity/src/storage.rs>

Can someone capable with Rust provide a simple example of doing this that can be generalized for any custom `struct` or data structure?

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [July 5, 2022, 3:33pm UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/2 "2022-07-05T15:33:39Z")

</div>

Pssst, hey!

> **[GitHub - seniorjoinu/ic-stable-memory: Lets you store canister data directly...](https://github.com/seniorjoinu/ic-stable-memory)**
>
> Lets you store canister data directly in stable memory - GitHub - seniorjoinu/ic-stable-memory: Lets you store canister data directly in stable memory

Tutorials are on their way!

---

<div class="post-metadata">

**Author:** ![saikatdas0790](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/saikatdas0790/32/17147_2.png) [@saikatdas0790](https://forum.dfinity.org/u/saikatdas0790)\
**Post date:** [July 6, 2022, 7:27am UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/3 "2022-07-06T07:27:49Z")

</div>

Hi @senior.joinu

Thank you so much for this.

I went through the documentation for it and had a couple of questions:

- In the first example as shown below  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/b/bc621ec98c744f8c7695697aa7b7ba7ae26965f5.png)  
we can directly set to and retrieve from stable memory using the `get_var` and `set_var` functions provided. Will this support all the collections from the standard library or is it only limited to specific data structures?

- In the third example as shown below  

- Are there plans to create stable variants of other data structures such as a `BTreeMap` or a `Set`?

- How does one handle changes to the schema? If I were to store a `User` struct with `name` and `age` in a `SHashMap` today and then later decide to add another field `gender` to the `User` struct later, how would I migrate the existing collection?

Again, thank you for sharing your expertise. Highly grateful for the insights 🙂

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [July 6, 2022, 12:32pm UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/4 "2022-07-06T12:32:10Z")

</div>

Hi @saikatdas0790

> [@saikatdas0790](#):
>
> Will this support all the collections from the standard library or is it only limited to specific data structures?

Any structure that implements `CandidType` trait will work fine. So any standard type that implements it should also work out of the box. For those types, which don’t implement this trait (as BinaryHeap, for example) you may just want to make a wrapper type that will implement it.

> [@saikatdas0790](#):
>
> how does the calling code determine the `from` and `to` values to be passed? Is there some adjoining index mechanism that is available to the calling code that can help it determine what to pass?

Oh, no. These “from” and “to” variables are just regular code. There is no notion of pagination in ic-stable-collections.

> [@saikatdas0790](#):
>
> Are there plans to create stable variants of other data structures such as a `BTreeMap` or a `Set`?

It is possible to add a support for `HashSet` pretty quickly. But `BTree`s seem quite tricky for me to implement, so it would take some time. But, yes, BTree-based collections are the first in the queue.

> [@saikatdas0790](#):
>
> How does one handle changes to the schema? If I were to store a `User` struct with `name` and `age` in a `SHashMap` today and then later decide to add another field `gender` to the `User` struct later, how would I migrate the existing collection?

There are options. You can define your struct as a versioned box beforehand. E.g.

```rust
struct UserV1 {
  name: String;
}

struct UserV2 {
  name: String;
  phone_number: String;
}

struct User {
  version: u16;
  data: Vec<u8>;
}

```

Then you can serialize `UserV{X}` into the `data` field of `User` struct and type a correct version number.

Also, you can just introduce a new collection instance for each new version.

```rust
struct UserV1 {
  name: String;
}

struct UserV2 {
  name: String;
  phone_number: String;
}

let users_v1 = SHashMap::<ID, UserV1>::new();
let users_v2 = SHashMap::<ID, UserV2>::new();

```

But in general, you should be able to use any other data migration technique that you’re using right now with common collections.

---

<div class="post-metadata">

**Author:** ![saikatdas0790](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/saikatdas0790/32/17147_2.png) [@saikatdas0790](https://forum.dfinity.org/u/saikatdas0790)\
**Post date:** [July 6, 2022, 1:17pm UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/5 "2022-07-06T13:17:45Z")

</div>

Thank you so much for the response and the explanations.

I will definitely give this a try and provide any feedback that I have.

Cheers

---

<div class="post-metadata">

**Author:** ![saikatdas0790](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/saikatdas0790/32/17147_2.png) [@saikatdas0790](https://forum.dfinity.org/u/saikatdas0790)\
**Post date:** [July 30, 2022, 5:10pm UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/6 "2022-07-30T17:10:27Z")

</div>

Hi @senior.joinu

I tried using it and ran into a snag. I created an issue for it here

> <https://github.com/seniorjoinu/ic-stable-memory/issues/2>
>
> I'm running into this error when I add \`ic-stable-memory\` to my \`Cargo.toml\` pac…kages.
> 
> !\[image\](https://user-images.githubusercontent.com/7412443/181933969-73215b40-ca0e-42e4-9a46-6baa717149d8.png)
> 
> 
> Do I have to switch to the \*\*nightly\*\* release channel to use this library or am I doing something wrong?

Let me know if there’s any other details I can provide. Thank you for the awesome lib 🙂

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [July 30, 2022, 8:24pm UTC](https://forum.dfinity.org/t/rust-using-stable-storage-as-main-storage-example-pattern/14154/7 "2022-07-30T20:24:12Z")

</div>

Hey. Thanks for trying it out and opening the issue.

Yes, for now you need a nightly version of Rust.  
I’ll see what I can do in order to make it work with stable!
