I want to draw attention to a new page in documentation that was published with little fanfare. @thisisjoshford did a great job explaining data structures and collections on NEAR here: https://docs.near.org/docs/concepts/data-storage#rust-collection-types
Take a look at the charts he generated for the Rust SDK collections:
Writing smart contracts in Rust scared me at first. If you open a random file in the @NEARProtocol node codebase, it can be intimidating. Luckily, the Rust crate near-sdk removes the trickiest parts for you. https://crates.io/crates/near-sdk 
It reduces the cognitive overhead so much…
that I believe all you need to know is captured in the first few chapters of the official Rust book here:
https://doc.rust-lang.org/book/ch01-00-getting-started.html
Chapters 1-3: you'll be comfortable.
Chapters 4-6: real cozy.
The problem we've had at NEAR, however, has been that these chapters use HashMap a lot.
In blockchain world, we need to be quite cognizant of collections and how they're loaded into memory. The standard collection HashMap will load the entire thing into memory. Maybe this works fine if you're storing info about DOTA 2 teams, where each team will always have 5 folks,
but not fine when you want to have a fungible token that stores hundreds of thousands of key/value pairs. This is where good ol' computer science comes in handy. Note the AVL Tree here:

(No NEAR collections use BST, but added for comparison/clarity here.)
In the image from the first tweet, you can see the Rust "tree_map" is on a different Y-scale, costing more gas. This makes sense when you see the extra CPU cycles it takes in the animation. More gas on insertion, but slightly cheaper upon data retrieval due to rebalancing in vid.
Oftentimes, developers will ask, "If I shouldn't use HashMap for this, which collection should I use?"
The — somewhat unsatisfying — answer is, as always, "it depends what you want to do with it." There's a handy chart on this docs page for that:
Do you absolutely need to use NEAR collections? No. @ekuzyakov did an amazing video on a fungible token contract he wrote in Rust without the SDK at all. (Also without standard imports or "no_std")
This is using registers to store and retrieve only 🤯
Check out that page in docs and if you want more technical details on collections, the Rust docs for the crate are super useful:
https://docs.rs/near-sdk/2.0.0/near_sdk/collections/index.html
If this is your first time writing Rust, trust me, you can do it. The pattern recognition part of your brain is all you need :)
You can follow @mikedotexe.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.