Intro to Rust Smart Contracts With Arbitrum Stylus - ETHGlobal Bangkok 2024
ETHGlobal·Thu, Oct 9, 2025, 12:00 AM
Join us for a workshop with Chris Cordle from Arbitrum titled, "Intro to Rust Smart Contracts With Arbitrum Stylus." Using Rust to build smart contracts gives you superpowers. Unlock 1000x more memory and 100x more compute with Arbitrum Stylus. We'll cover the basics of Rust and how to deploy and interact with your app. This workshops took place at ETHGlobal Bangkok, a 3-day hackathon held November 15 to 17 2024, that brought together the most skilled web3 developers, designers and product builders from all around the globe for a weekend-long adventure to advance the Ethereum ecosystem! ----------------- Follow us: https://x.com/ethglobal Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com
Transcript
hello uh welcome to uh intro to rust Mar contracts with arbitrum stylus um so in this uh Workshop uh I hope to go over just how to do kind of basic rust and just get you kind of on ramped enough to you know kind of understand the easy parts of rust I guess enough to like get you comfortable if you're working on a hackathon project for um we've got like uh arum has $188,000 uh towards stylus grants uh for this hackathon so and and usually these uh these tracks don't get a whole lot of applications compared to a lot of other ones so usually if you submit something you got a pretty pretty decent chance of of winning a prize so uh this can be a highly useful skill and it's something that I hope like if you know another language and you're not really familiar with rust I hope you can like at least um get understand a couple of the basic parts of rust to um you know like you if you know something else like Java JavaScript you should be able to get on- ramped enough to build something for this hackathon and put something together um and it it it shouldn't be too intimidating for you so we'll just uh we'll kind of stick with Russ the easy parts for this demo but um uh but yeah that's my name there I go by Chrisco um Chrisco 512 on uh Twitter and also telegram feel free to DM me uh yes oh I'm sorry share something yes that right yeah I'm just yeah yeah I'm about to uh change the side sorry all right thanks man um yeah uh so what is stylus by the way if uh for those not familiar it's a parallel VM that runs on arbitrum chains and it's essentially it runs as a sister VM to the evm on arbitrum chains and that means that you can uh when when a when a contract is compiled in wasm and deployed on chain there's a small little header that gets deployed with the bik code and arbitrum will either run it in a uh wasm context in the wasm VM or the evm depending on um what that b code gets interpreted as and it remains fully composable with the evm uh so it's not like a separate execution environment alog together it still shares a storage state with uh the evm so you're able to store uh data in the same code locations as other uh sty or solidity data so it's just fully composable with solidity and um as you can see other languages are supported we're going to go over rust here but you could also use Zig or C or assembly script or any other like systems typee language that compiles to web assembly could be very um powerful for that but for um we just built the rust SD for now as a as a central Focus for us but we are excited to support other languages in the future but for now we just got a our rust SDK to use but the main benefits of stylus is that you get access to 70 times more compute per unit of gas versus the evm and a thousand times more memory and that's because the web assembly VM runs closer to like bare metal assembly instru instructions and so there's less virtualization and things like that going on in the arbitrum nodes and we pass those gas savings on to developers um and to users of the blockchain so whenever you're doing something computationally heavy um Stylus is a prim you know uh that's that's where it shines um storage costs tend to remain the same as solidity cost so if it's just a storage head be or just it only has like a few additions subtractions that sort of thing um it you know might not be a big gas savings versus solidity but it still can enable these other languages if you want to build in other types save languages like rust um so it can be useful for just for writing safer contracts and things like that as well um yes going over it supports alternative coding paradigms helps onboard developers from web 2 uh and we want to get access to the 20,000 rust devs that are out there and um you know there's also hundreds of thousands of other developers in other languages we'd like to onboard so um these are the these are kind of why we built stylus on arum all right so to get started uh with it um your primary resource for working with stylus will be docstar arbitrum do and uh in there on the side bar we have a section called right uh rust smart contracts or right stylus rust smart contracts I forget what it says but it'll be on the Side Bar you click into that and if you go into the quick start you'll be able to you can follow that to like get all the prere installed in your system you'll need rust installed you'll need Docker installed on your local system and uh rust up is the CLI tool there's a couple CLI tools that you'll that will come in handy but just follow that these instructions on there and um it's very easy and quick to to get going um yeah there's another section of the docs called run a local Dev node and that's going to explain how to get a virtual arbitrum node set up on your local system so for a quick deployments and for the purpos while you're at this hackathon you'll just want to work locally against your local um stylus Dev node or arbitrum Dev node um so look for that section e either on their the stylus section or I think it's also somewhere else but um also if you uh for the purposes of the hackathon if you want to submit on the price track you'll want to deploy it to arb's deoya and here are three uh faucets you can use these faucets are also listed on our on the doc star. so um if you need some test net eth and also if if you need more than what the faucets are giving you just reach out to us or come to the booth and and uh we'll send you some test ID so just let us know okay so again rust primary advantages over many other languages uh or some other languages is that it's a very strongly typed language it's memory safe it's got a lot of highlevel language features so it kind of feels more like a modern language like Java or C or um it's got a lot of highlevel features but it is it still remains a systems level language so it runs close to the speed of C or C++ um so it's very fast it's very efficient so uh rust has been growing rapidly over the past few years uh for those reasons um it also has a very powerful compiler that will a lot of times just let you know specifically what line of code is incorrect and so it's a very helpful compiler um it also has like some cool features like pattern matching um as I said it's very fast um these are kind of how you work with variables in uh rust you can either uh Define variables are actually uh immutable by default So when you say let x equals 5 that's an immutable value but if you say let mute xals 5 that's a mutable value now and you can now mutate that that value these are some of the data types that um that you have access to by default in Rust uh these are unsigned integers uh by default rust supports all the way up to unsigned integers of 128 bits um but for stylus uh we have a library that we bring in called alloy that's from The Foundry team but uh we use it as part of our SDK to give support for 256 bit integers since um you need to maintain that solidity compatibility uh but these are this lowercase U these are the native rust types so sometimes when you're working with SDK you'll need to uh parse from native rust to solidity types um but I'll get into that later U later in the demo but these are just the native rust types that you kind of have access to this is what booleans characters tupal look like a tupal is just like kind of a set of values um and arrays um this is what a function looked like it's very simple FN main you uh print line call a function so again this is just working with functions very simple and for for the most part for the hackathon and and whatnot I would recommend just kind of sticking with basic functions calling other functions um just to keep your your code simple and um and straightforward um this uh this Arrow syntax um where you see the plus one function down there that's how you return a value so you just use that Arrow syntax to indicate the return uh this is a struct so this is if you're setting up uh a data struct uh you'll us structs quite frequently in Rust um this is just a rectangle with a width of you with a 32bit unsigned integer for both width and height um it's very similar to objects in oop languages like uh Java or whatever but uh they are slightly different um Russ tends to uh isue things like uh inheritance uh as a design principle but uh so a lot of times with uh rust uh adopts something called composition over inheritance so a lot of times you'll just attach methods to strs um and you'll Implement traits but um I don't know if I'll get into that too much in this talk but um yeah like just just know traits are kind of like interfaces in other languages and uh you can Implement traits for strs in order to allow create behavior for the rectangle like a Computing area or something like that um yeah this is just uh giving an example of declaring a a rectangle there's multiple ways to do this um but this is just the one of the default ways to declare a rectangle and instantiate it um yes what I mentioned earlier with impling so what right here what we're doing is we're creating an area method on this rectangle so we've got the rectangle struct that we defined above and then the imple rectangle um um imple you can think maybe Implement I you know like it's just imple rectangle um this is how you by default attached methods to a struct so you just imple for that struct and then you just Define your functions down below and that and self is a reference you know that's uh that's just a reference to the instance of the rectangle itself so that's always the default first brain ameter and it's a reference there um that that you reference for the first parameter on the on the method um so that's very straightforward self. width self. height gives you the area which returns a u32 uh rust also has access to something called anes uh ANS are very powerful in Rust uh they're very simple to declare uh you can also imple things for NS um uh right here and it also ns are very powerful for uh pattern matching and rust so what you see right here is an example of pattern matching where we got a coin type uh coin in and then we've got value and sense and we're just part matching on that value and then we've got all the all the matching arms of that Ane that that could possibly be and rust uh forces you to always handle all the possible uh enumerations so you always have to either explicitly declare each or you can provide a default um a default arm on that match statement if uh if uh if you need to cover just the basic default use case if you only wanted to find one for like 10 for example okay so this is some uh control flow kind of more advanced control flow type things you can do you can say let config Max equals sum 3 u8 so what that statement is saying is that if uh that config Max is going if it has a value like if this is a rust has this option type where it can either be none or something and that some keyword is just like it will if this value has something in it and it's not uh if it's not the nun type then um it will match on that Su branch and it will what the what this is doing is matching on config Max which we already assigned it the value of some 3u8 38 is an 8bit unsigned integer with the value of three and we're matching on that config Max and then given that it is something we're going to parse that value out as Max and in that matching Branch line right there where it says print line that would print out the the number three so yeah this is just a little bit more advanced Sy txt but um takes a little bit getting used to but um it could be uh very elegant um when you need when you need to make use of this but highly recommend learning pattern matching as you get more advanced in Rust um as I said earlier traits are kind of like similar to interfaces and oop languages so this is how you Define behavior for structs or NS and uh so right here I've got this summary trait with a summarized me method and I'm just declaring the interface for that method it doesn't take any parameters it receives a reference to self but it returns a string so that's the summary trait all right and this is an example of that in use where I've got a tweet struct with username content uh reply and retweet and I'm then implementing the summary trait for tweet and on that method so that I'm implementing that summary trait and that gives now that tweet struct um has that behavior the summary Behavior so I'm always with an instance of tweet I can now call tweet. summarize and and get the username and content back um printed to the console that's essentially what this does right there uh macros are a powerful Concept in Rust um what this does is allows program code to be generated at compile time this is not something you'll learn if you're new to rust you you won't probably be able to learn this in this weekend for example but you will you make use of macros and you'll see them in practice so when you see this exclamation point like print line with an exclamation point on the end that is in a macro being invoked and also there's multiple types of macros that's the exclamation point indicates that it's a function like macro and so print line there is calling the print line macro function and then it's it's what that's going to do when your code compiles it's going to almost like generate code in the background and then it's going to like kind of remove that line and then stick that code right there before it compiles down into uh regular assembly or whatever so um so that's what you need to think of as ma when you see macros just note that in the background when you're compiling your codee's going to be sort of like swapped out with the code that gets generated by that macro and same thing with the hashtag with the brackets deriv debug or whatever if you see that syntax that's a macro and that means that uh the the block after that so that's usually like almost like an attribute like uh macro and those are usually attached to functions or attached to structs and that's just going to a lot of times it's just to like pre you know um it'll save you time from writing a lot of boiler plate that you would otherwise have to write so macros are really powerful really useful if you're new to them um just note that you know this is what's happening but uh it does take a while to fully wrap your head around macros if if you're totally new to them we do make a lot of use of macros in our SDK and I'll go over um some of those in just a second um okay so we wanted to do uh simple design patterns with our stylus reses K we made use of alloy which is a Primitives Library um that is uh gives you access to to swap between values between solidity compatible values and rust uh compatible values I'll show you that in just a second um when you're working with car stylus you'll want to use cargo stylus uh that is Cargo is rust CLI tool and uh if you just uh like if you want to create once you've installed that locally you can just call cargo stylus new my first contract or whatever and it will create a little basic counter contract locally and I'll show you that in just a second um as you're working with uh with your contract and as you're editing it you'll want to call Cargo stylist check every so often just to make sure you're fitting within the blockchain constraints because we do have some constraints on chain uh contracts need to be under 24 kilobytes um and we have to like we just have we do do some compression so it can be a little bigger than that we do run some onchain compression to get it try to get it under 24 kilobytes and running cargo stylus check we'll just make sure that your code is still under the limits if it's if you need more than that you'll have to break it up into multiple contracts um once you're ready to deploy you'll call Cargo stylus deploy with an endpoint a private key and it will deploy to uh either your local chain or um a remote chain um these are some of the Primitive types like I was saying earlier when you're working with stylist you'll use these alloy types so you'll use this capital u8 and this capital u 256 remember earlier it was lowercase U that was the native rust integers these are a library these are Library integers from our SDK but they are built on alloy um so these are Alloy types and essentially right here I'm I'm declaring 8 bit integer um from the number number one so that's just how you work with uh with our stylus SDK integers so sometimes you'll need to parse from one to the other and that's how you do it with that UA uh colon colon from one or whatever to um to press from native rust to solidity types um this is how you do addresses and uh in Rust you'll use uh like solidity style addresses you'll use that address type and you'll call colon from pass in well this is just like filling all 20 um all 20 bytes with ones so that's an example of how to do a quick uh a quick one but you could you could obviously fill out the whole range of the 20 bytes if you want it um right here is again local variables um oh yeah these are the different types of variables that you'll work with local variables which just use memory State variable which you state and store on chain and then Global variables which are um values available in the VM so those are usually referen from within the SDK itself you'll just pull those in as Library values and then you can reference those um okay remember earlier I was showing you that hashtag with the um as a macro type so that what you're seeing right here is solidity storage macro and what that means is we're declaring a struct type right here this is us setting up our state for our smart contract for how our state's going to be laid out and that solidity storage macro is is saying to R this is going to this is going to lay out everything in a solidity compile way uh compatible way this is also going to be the the entrypoint struct there should be one exactly one entry point struct in your smart contract and that's kind of your top level struct that's your root level struct if you want to think of it that way you can have other solidity storage structs but you just need one entry point struck and so that's going to be the route and so your your storage is going to start with that initialized variable the owner variable then the max Supply variable so you've got a Boolean there an address and then a U2 56 and those are the storage State on on chain all right when you're actually I need to update the slide sorry this uh this external macro is now called public but I think both will still work but uh we did deprecate externals just to kind of match more with solidity so um use public when you're and if you'll see it if you just clone the the default counter um it'll show public so stick with that but that public method uh that public macro or that external macro um what we're doing there is we're impling on counter and we're we're setting up our public methods that are expose our basically our a API or solidity API for um for methods that external users can call on your smart contract all right let's jump into the code um yeah yeah um yeah those are price tracks but let's jump into the code I've only got a few minutes so I want to hop over to that real quick um okay so real quick I'm going to do cargo stylists so this is once you you this is assuming you have everything set up I'm going to say cargo stylus new counter demo right here and that should clone in and I think I should have it over here all right so I'm going to click into li.
RS is the default like main file that you'll be editing on your smart contracts and um just kind of walk you through this real quick there's some comments up here here we'll skip past these comments a lot of this is boilerplate code that you'll just leave in place this is just uh so that you can export the Abbi for your uh contract later so kind of just leave that in place these are dependencies we're pulling in from the stylus SDK we're just pulling in unsigned 256bit integers from the alloy Primitives library and then this Prelude star is just kind of bringing in a lot of different things that we can just reference like this entry point macro and stuff like that these commonly used things that we need access to so that star just brings in those dependencies for us um what you see here is this S Storage U macro and this allows you to cleare your types in a solidity compatible way right so I think on this slide I use the rust style way there's two different ways to do it but this using this solidity style you'll actually use solidity style syntax to declare your um state to lay out your state so um so that's exactly we're essentially within this writing out um solidity like U 256 solidity uh so that's a that's a 256 bit number that it's going to track the state of our counter again we attached this entry point so that is the root level struct the root level storage struct for our smart contract okay and then again remember I said it was external before but it's public now so just stick with public and we're impling for that counter up here that we declared and we've got a number of public methods available including number where we're able to get the number from the storage the value of the storage we're able to set the number to whatever we want we can multiply it by a number we can add a number or we can just increment it by one all right so the way that works in practice so like yeah right here um we made these public and that means it's going to get exported on the ABI we also for mutable functions so this and mute syntax and mute space self that means this function is a uh mutable function it mutate state or it's able to mutate state so when we're calling self. number. set new number we are we are setting that number to that value in storage um so on chain that will be stored on chain to whatever we pass in okay um getting real close to time so I'm going to re through this real quick but uh CDLs uh CD counter demo okay cool and we got um cargo stylist deploy okay so I think locally I have like some environment variables set up this no verify syntax is just went um there was a contract verification that can run locally that can take a little bit of uh compute time if you you want to just deploy real quick you can skip the verification step that'll come later if you want to verify your contract on ether scan or whatever um but then yeah I'm passing in the endpoint with the local RPC variable and the private key which is provided to you by when you set up a local Dev node if you look on the docs um there's a private key that's given to you with local local eth that you can use for that so you'll use that private key to deploy and it'll deploy locally it look like this and once it gets deployed should be quick it's compiling deployed okay came out 7 kilobytes so we're going to copy that address there and I'm using the library called cast which is from Foundry so um I'm going to set this to our new contract and then so cast is a great tool if you're um you know if if you're uh for E either if you're working with solidity or whatever you're working with so um it it's a great tool for interacting with onchain contracts from the CLI so oh I I meant to do call there I did send um so real quick I'm just going to call the counter it should come out to zero all right and then when I go up here and I click uh this time I'm going to do increment and then I'm going to cast sent increment okay so that actually worked on chain and then uh cast call now if we call the number again it should say one right so it came out to one there so again if I increment again it should be now two right so that's how you you know that's just kind of how you build this is very you know um I think this is very approachable if you kind of stick with the you know the easy parts of rust uh rust can be intimidating some of the more advanced parts of us but uh I think you know if this is new to you and everything um you kind of kind of work start with the basic contract and then mutate add functions uh that that accomplish what you want but um yeah I think it can be doable if even if you're brand new to rust in the in in weekend so yeah so you can see a read uh yeah yeah you can add Right add numbers right there right so if I wanted to I know yeah yeah oh okay oh I see um yeah you would just do this you would do um right right here you would just say um yeah you would just say U 256 from two does that make sense yeah okay um yes thank you for that question and sorry I'm unfortunately I ran out of time but uh appreciate y'all coming I'm happy to answer any questions afterwards thank you
Automatic transcript — names and jargon may be misspelled.