Cairo lang Hello World for JS developers
Jorge Zerpa (zerpacode)
Posted on September 10, 2024
When you start learning a new coding language, the more tedious part (by far) is to deal with long tutorials where the most part of the time is expended on explain basic coding concepts and not focused on explain such language features and syntax.
So for this series of posts, I will assume that you already know how to code and that you understand all basic concepts as variables, functions, loops, etc. So we can move on so much faster.
In this first chapter
Getting needed tools
In JS you have tools like npm or yarn that helps you to manage your projects (manage dependencies and versions, scaffold a basic project structure, etc).
In Cairo, we have scarb, a CLI tool to abstract a lot of work for us.
Scarb already has a Cairo compiler and all tools you need so you can start writing Cairo code in a very straight forward way.
You can download Scarb from here.
Your Hello World in Cairo
First, create a folder when you want to store your Cairo projects, and then execute this command from the terminal scarb new hello_world
. This will scaffold for you a new project with the next structure:
You will see a parent folder called "hello_world" or whatever name you pass to the command scarb new the_name_of_your_project
.
Inside, there is an Scarb.toml file, which should looks something like this:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2024_07"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
[dependencies]
[dev-dependencies]
cairo_test = "2.7.1"
this file is the equivalent to the a package.json file in JS, has the basic project information like name, version and dependencies.
at the same level you will find a Scarb.lock which is the same as the package.lock or yarn.lock file in a JS project, it does contains the exact versions of the dependencies used.
Now let's go into the src folder, here is where your project lives. Inside there is a lib.cairo file. This is the entry point of your project (like an index.js, for example).
Now, remove all the code inside it and paste the next one:
fn main () {
println!("Hello World!");
}
The code above has a main function, that is always the first code executed in a cairo program. Inside there is a call to another function println! which is like a console.log().
Now we have everything ready to run our program, to execute it, run "scarb cairo-run" from your terminal (remember that you have to execute it in the same directory where your scarb.toml file is).
If everything goes well, you will see a "compiling" message on the console, followed by your hello world!.
So congrats! you made your first hello world in Cairo, and have a basic setup to continue practicing.
On the next posts we will cover other basic topics like how to work with variables, functions, etc. And more advance topics like ownership, snapshots, testing and so on. Stay tuned and Happy Coding!⭐
Posted on September 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024