My story when started learning rust

Nasri Adzlani
3 min readMay 5, 2022

--

rust programming language icon

Im write this at Eid, Happy Eid ul-fitr, May Allah bless you immensely!

I a solidity and Ethereum blockchain developers btw on my main job, but at my side job as a beginner Solana developers, solidity has a converter from solidity into Solana named solang, but solang cant handle like a spl-token transaction etc, That’s why I decided to study rust to create and learn smart contract running at solana network.

Im use macbook air with m1 chip to develop program with rust, if u using any os maybe there will be a different way of installing and the error that occurs.

Learning stated

Reference: https://www.rust-lang.org/tools/install

installing

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

copy and and paste into your terminal
after successfully installed rust u must be setting environment

$ export PATH="$HOME/.cargo/bin:$PATH"

checking first that rust has been installed or not

$ rustc -V
rustc 1.60.0 (7737e0b5c 2022-04-04)

if rustc version show, congrats your rust has been installed on your device.

Hello World….

in my device, vscode insider more stable than classic edition. and im use for text editor.

create folder ‘hello_world’ for your testing program directory

$ cd Documents
$ mkdir hello_world
$ cd hello_wold
$ code .

code . is a command for open vscode on this folder.
create some a rust file with name “main.rs”

fn main(){
print!("Hello");
println!("Hello World")
}

like a java program, print use to print text without enter, but println with enter.
To running this program u can use rust compiler to compile into execute program.

$ rustc main.rs

after you run compile command, rust will be create a new file with name ‘main’, or ‘main.exe’ if u running at windows.
to run your program you can use command below

$ ./main

if u have error about permission u can change permission with

$ chmod +x main
$ ./main
example running

Cargo

Cargo is Rust’s build system and package manager. Most rust developers use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries.

$ cargo --version

if u see a version, you have it!

Creating project with cargo

Let’s create a new project using Cargo and look at how it differs from our original Hello, world! project

$ cargo new test_cargo

after run this command, will be created ‘test_cargo’ folder, inside folder have a src folder, toml file and .gitignore file.
like a node, Cargo.toml is like package.json to manage dependency name version and edition package.

you can edit rust file in src/main.rs with default main function.

fn main() {
println!("Hello, world!");
}

Build and running cargo

From your hello_cargo directory, build your project by entering the following command:

$ cargo build

This command creates an executable file in target/debug/test_cargo.

We just built a project with cargo build and ran it with ./target/debug/hello_cargo, but we can also use cargo run to compile the code and then run the resulting executable all in one command:

$ cargo run

Cargo also provides a command called cargo check. This command quickly checks your code to make sure it compiles but doesn’t produce an executable:

$ cargo check

Data Type

Rust have a 2 subsets, is a scalar and compound types. Keep in mind that Rust is a statically typed language, which means that it must know the types of all variables at compile time. The compiler can usually infer what type we want to use based on the value and how we use it.

Scalar type

scalar type represent a single value and have a four types: integer, float, boolean and char.

Integer

continuation will be added

--

--

Responses (1)