BOOK THIS SPACE FOR AD
ARTICLE ADLearn the basics on how to start deploying Rust smart contracts
First thing if you haven’t already is to install Rust compiler into your system :)
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shThat will install Rust and Cargo.
After that we can update all Rust dependencies with:
$ rustup updateTo confirm that everything was fine:
$ rustc --versionrustc 1.80.0 (051478957 2024-07-21)
Now with cargo we can create our first Rust project:
$ cargo new rust-project-1Creating binary (application) `rust-project-1` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
$ ls rust-project-1
Cargo.toml src
Inside src/ folder we can find a rust Hello World program:
$ ls rust-project-1/srcmain.rs
$ cat rust-project-1/src/main.rs
fn main() {
println!("Hello, world!");
}
Now with cargo we can compile and run the program:
$ cd rust-project-1$ ls
Cargo.toml src
$ cargo build
Compiling rust-project-1 v0.1.0 (/home/ziion/rust-project-1)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/rust-project-1`
Hello, world!
If your are using Visual Studio Code, you can try with this extension:
https://marketplace.visualstudio.com/items?itemName=dustypomerleau.rust-syntaxhttps://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzerTime to try with our first Rust smart contract!
We will add this to our target:
$ rustup target add wasm32-unknown-unknownAnd also:
$ cargo install cargo-make$ cargo install wasm-tools
To continue, just copy the code below into src/main.rs:
#[derive(Debug)]struct BasicContract {
value: u32,
}
impl BasicContract {
/// Constructor to initialize the contract with a value
fn new(initial_value: u32) -> Self {
Self { value: initial_value }
}
/// Method to get the current value
fn get_value(&self) -> u32 {
self.value
}
/// Method to set a new value
fn set_value(&mut self, new_value: u32) {
self.value = new_value;
}
}
fn main() {
// Simulate contract creation with an initial value
let mut contract = BasicContract::new(42);
println!("Initial value: {}", contract.get_value());
// Simulate updating the value
contract.set_value(100);
println!("Updated value: {}", contract.get_value());
}
As you can see it is a really simple smart contract, where we store an initial value of 42 and later on we update that value to 100.
Now we can compile and execute the smart contract with cargo run:
$ cargo runFinished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
warning: the following packages contain code that will be rejected by a future version of Rust: syn v0.14.9
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
Running `target/debug/rust-project-1`
Initial value: 42
Updated value: 100
Eureka!
For additional posts, we will learn how to audit and deploy Rust smart contracts in different blockchains.