DEV Community

Alejandro Huitemagico
Alejandro Huitemagico

Posted on • Edited on

Echo2: A Tiny Piece of Code for Learning and Practicing Soroban Rust SDK

echo2 state machine diagram
Hello everyone,

To facilitate the learning process of the Soroban SDK https://soroban.stellar.org/docs/reference/sdks/rust and provide to the beginners, like myself, with an example, I have created a program called "echo2".

Note: If you want to access a comprehensive documentation for installing all the necessary tools to run 'echo2,'
please refer to echo2tutorial.pdf.

(1) name

echo2 "tiny piece of code for learning and practice soroban rust sdk"
version 1.1

(2) What is the function of the echo2 program

This program takes a text parameter as input from the caller, retrieves the previous message from
persistent storage associated with the contract, and then saves the new message.
So, with each subsequent call, 'echo2' returns 'the echo of your message' along with the previous message.

echo2 sequence

echo global diagram

For the community of Soroban programmers, I believe there is a significant opportunity for creating more basic examples, with documentation, and with that, making the code writing process with the Soroban SDK much easier.

You can find the code and documentation of echo2 program at: (https://github.com/huitemagico/echo2)

(3) Echo2 as an finite state machine

Echo2 is a program that implements a basic state machine with two states: reset_state and echo_state.
In the reset_state, the program does not process the previous message but instead resets it with a special meaning.
If Echo2 receives another reset message while in the reset_state, it remains in that state.
However, if it receives a message different from "reset," it transitions to the echo_state while saving the previous state and counter.
While in the echo_state, if Echo2 receives a general message that is different from "reset," it remains in that state.
The machine continues to stay in the echo_state until it receives a "reset" message, at which point it transitions back to the reset_state.
The first diagram describes this.
Note: The explanation of a state machine is easiest using a "finite state machine diagram" for better understanding.
See "finite state machine diagram"

(4) Steps for run the program:

Note: If you want to access a comprehensive documentation for installing all the necessary tools to run 'echo2,'
please refer to echo2tutorial.pdf.

(5) Some urls

Storage
https://docs.rs/soroban-sdk/latest/soroban_sdk/storage/index.html

Persisting Data
https://soroban.stellar.org/docs/fundamentals-and-concepts/persisting-data

Struct String
https://docs.rs/soroban-sdk/latest/soroban_sdk/struct.String.html

Soroban struct Vec
https://docs.rs/soroban-sdk/latest/soroban_sdk/struct.Vec.html

Struct Env
https://docs.rs/soroban-sdk/latest/soroban_sdk/struct.Env.html

Soroban CLI
https://soroban.stellar.org/docs/getting-started/setup#install-the-soroban-cli

Soroban examples doc
https://soroban.stellar.org/docs/basic-tutorials/events

Soroban examples
https://github.com/stellar/soroban-examples

Rust doc
https://doc.rust-lang.org/std/index.html

Soroban Rust sdk
https://soroban.stellar.org/docs/reference/sdks/rust

Github url
(https://github.com/huitemagico/echo2)

THANKS FOR READING!

Top comments (0)