Hello, World
Scarb aracılığıyla Cairo'yu kurduğunuza göre, ilk Cairo programınızı yazma zamanı geldi. Yeni bir dil öğrenirken geleneksel olarak ekrana Hello, world!
metnini yazdıran küçük bir program yazılır, biz de burada aynısını yapacağız!
Note: This book assumes basic familiarity with the command line. Cairo makes no specific demands about your editing or tooling or where your code lives, so if you prefer to use an integrated development environment (IDE) instead of the command line, feel free to use your favorite IDE. The Cairo team has developed a VSCode extension for the Cairo language that you can use to get the features from the language server and code highlighting. See Appendix F for more details.
Proje Dizini Oluşturma
Cairo kodunuzu saklamak için bir dizin oluşturarak başlayacaksınız. Cairo için kodunuzun nerede bulunduğu önemli değildir, ancak bu kitaptaki alıştırmalar ve projeler için, ana dizininizde bir cairo_projects dizini oluşturmanızı ve tüm projelerinizi orada saklamanızı öneririz.
Bir terminal açın ve bir cairo_projects dizini oluşturmak için aşağıdaki komutları girin.
Linux, macOS ve Windows'ta PowerShell için şunu girin:
mkdir ~/cairo_projects
cd ~/cairo_projects
Windows CMD için şunu girin:
> mkdir "%USERPROFILE%\cairo_projects"
> cd /d "%USERPROFILE%\cairo_projects"
Not: Bundan sonra, kitapta gösterilen her örnek için, bir Scarb proje dizininden çalışacağınızı varsayıyoruz. Scarb kullanmıyorsanız ve örnekleri farklı bir dizinden çalıştırmayı denerseniz, komutları buna göre ayarlamanız veya bir Scarb projesi oluşturmanız gerekebilir.
Scarb ile Proje Oluşturma
Scarb kullanarak yeni bir proje oluşturalım.
cairo_projects dizininize gidin (veya kodunuzu saklamaya karar verdiğiniz herhangi bir yere). Sonra aşağıdaki komutu çalıştırın:
scarb new hello_world
Scarb will ask you about the dependencies you want to add. You will be given two options :
? Which test runner do you want to set up? ›
❯ Starknet Foundry (default)
Cairo Test
In general, we'll prefer using the first one ❯ Starknet Foundry (default)
.
This creates a new directory and project called hello_world. We’ve named our project hello_world, and Scarb creates its files in a directory of the same name.
Go into the hello_world directory with the command cd hello_world
. You’ll see that Scarb has generated three files and two directory for us: a Scarb.toml file, a src directory with a lib.cairo file inside and a tests directory containing a test_contract.cairo file. For now, we can remove this tests directory.
Ayrıca bir .gitignore
dosyası ile birlikte yeni bir Git deposu başlattı
Note: Git is a common version control system. You can stop using version control system by using the
--no-vcs
flag. Runscarb new --help
to see the available options.
Open Scarb.toml in your text editor of choice. It should look similar to the code in Listing 1-1.
Filename: Scarb.toml
[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]
starknet = "2.10.1"
[dev-dependencies]
snforge_std = "0.38.0"
assert_macros = "2.10.1"
[[target.starknet-contract]]
sierra = true
[scripts]
test = "snforge test"
# ...
Listing 1-1: Contents of Scarb.toml generated by scarb new
Bu dosya, TOML (Tom's Obvious, Minimal Language) formatındadır, bu da Scarb'ın yapılandırma formatıdır.
İlk satır olan [package]
, takip eden ifadelerin bir paketi yapılandırdığını belirten bir bölüm başlığıdır. Bu dosyaya daha fazla bilgi ekledikçe, diğer bölümleri de ekleyeceğiz.
The next three lines set the configuration information Scarb needs to compile your program: the name of the package and the version of Scarb to use, and the edition of the prelude to use. The prelude is the collection of the most commonly used items that are automatically imported into every Cairo program. You can learn more about the prelude in Appendix D.
The [dependencies]
section, is the start of a section for you to list any of your project’s dependencies. In Cairo, packages of code are referred to as crates. We won’t need any other crates for this project.
The [dev-dependencies]
section is about dependencies that are required for development, but are not needed for the actual production build of the project. snforge_std
and assert_macros
are two examples of such dependencies. If you want to test your project without using Starknet Foundry, you can use cairo_test
.
The [[target.starknet-contract]]
section allows to build Starknet smart contracts. We can remove it for now.
The [script]
section allows to define custom scripts. By default, there is one script for running tests using snforge
with the scarb test
command. We can also remove it for now.
Starknet Foundry also have more options, check out Starknet Foundry documentation for more information.
By default, using Starknet Foundry adds the starknet
dependency and the [[target.starknet-contract]]
section, so that you can build contracts for Starknet out of the box. We will start with only Cairo programs, so you can edit your Scarb.toml file to the following:
Filename: Scarb.toml
[package]
name = "hello_world"
version = "0.1.0"
edition = "2024_07"
[dependencies]
Listing 1-2: Contents of modified Scarb.toml
Scarb tarafından oluşturulan diğer dosya src/lib.cairo adını taşıyor, şimdi tüm içeriği silelim ve aşağıdaki içeriği ekleyelim, nedenini daha sonra açıklayacağız.
mod hello_world;
Ardından src/hello_world.cairo adında yeni bir dosya oluşturun ve aşağıdaki kodu içine ekleyin:
Filename: src/hello_world.cairo
fn main() {
println!("Hello, World!");
}
Şimdi lib.cairo adında bir dosya oluşturduk, bu dosya başka bir hello_world
adlı modülü referans alan bir modül bildirimi içeriyor. Ayrıca hello_world
modülünün uygulama detaylarını içeren hello_world.cairo dosyasını içeriyor.
Scarb, kaynak dosyalarınızın src dizini içinde bulunmasını gerektirir.
The top-level project directory is reserved for README files, license information, configuration files, and any other non-code-related content. Scarb ensures a designated location for all project components, maintaining a structured organization.
Eğer Scarb kullanmayan bir projeye başladıysanız, onu Scarb kullanan bir projeye dönüştürebilirsiniz. Proje kodunu src dizinine taşıyın ve uygun bir Scarb.toml dosyası oluşturun. Ayrıca src klasörünü ve içeriğini içeren Scarb.toml dosyasını oluşturmak için scarb init
komutunu da kullanabilirsiniz.
├── Scarb.toml
├── src
│ ├── lib.cairo
│ └── hello_world.cairo
A sample Scarb project structure
Bir Scarb Projesi Oluşturmak
hello_world dizininizden aşağıdaki komutu girerek projenizi oluşturun:
$ scarb build
Compiling hello_world v0.1.0 (listings/ch01-getting-started/no_listing_01_hello_world/Scarb.toml)
Finished `dev` profile target(s) in 8 seconds
This command creates a hello_world.sierra.json
file in target/dev, let's ignore the sierra
file for now.
Eğer Cairo'yu doğru bir şekilde kurduysanız, scarb cairo-run
komutunu kullanarak programınızın main
fonksiyonunu çalıştırabilir ve aşağıdaki çıktıyı görmelisiniz:
$ scarb cairo-run
Compiling hello_world v0.1.0 (listings/ch01-getting-started/no_listing_01_hello_world/Scarb.toml)
Finished `dev` profile target(s) in 15 seconds
Running hello_world
Hello, World!
Run completed successfully, returning []
Regardless of your operating system, the string Hello, world!
should be printed to the terminal.
If Hello, world!
did print, congratulations! You’ve officially written a Cairo program. That makes you a Cairo programmer — welcome!
Bir Cairo Programının Anatomisi
Hadi bu "Merhaba, dünya!" programını detaylı bir şekilde inceleyelim. İşte bulmacanın ilk parçası:
fn main() {
}
Bu satırlar, main
adında bir fonksiyon tanımlar. main
fonksiyonu özeldir: her yürütülebilir Cairo programında her zaman çalışan ilk kod parçasıdır. Burada, ilk satır, parametresiz ve hiçbir değer döndürmeyen main
adında bir fonksiyonu tanır. Eğer parametreler olsaydı, parantezlerin içine yerleştirilirdi ()
.
Fonksiyon gövdesi {}
içine alınmıştır. Cairo, tüm fonksiyon gövdelerinin etrafında süslü parantezler {}
gerektirir. İyi bir yazım stili, açılı süslü parantezi fonksiyon bildirimiyle aynı satıra koymaktır, araya bir boşluk eklenir.
Note: If you want to stick to a standard style across Cairo projects, you can use the automatic formatter tool available with
scarb fmt
to format your code in a particular style (more onscarb fmt
in Appendix F). The Cairo team has included this tool with the standard Cairo distribution, ascairo-run
is, so it should already be installed on your computer!
main
fonksiyonunun gövdesi aşağıdaki kodu içerir:
println!("Hello, World!");
Bu satır, bu küçük programdaki tüm işi yapar: ekrana metin yazdırır. Burada dikkat edilmesi gereken dört önemli ayrıntı vardır.
İlk olarak, Cairo stili sekme ile değil dört boşluk ile girinti yapmaktır.
Second, println!
calls a Cairo macro. If it had called a function instead, it would be entered as println
(without the !
). We’ll discuss Cairo macros in more detail in the "Macros" chapter. For now, you just need to know that using a !
means that you’re calling a macro instead of a normal function and that macros don’t always follow the same rules as functions.
Üçüncü olarak, "Hello, world!"
dizesini görüyorsunuz. Bu dizesini println!
'a bir argüman olarak geçiriyoruz ve dize ekranınıza yazdırılıyor.
Dördüncü olarak, satırı bir noktalı virgül (;
) ile sonlandırıyoruz, bu ifadenin sona erdiğini ve bir sonrakinin başlamaya hazır olduğunu gösterir. Cairo kodunun çoğu satırı noktalı virgül ile sonlanır.
Özet
Scarb hakkında şimdiye kadar öğrendiklerimizi özetleyelim:
- asdf kullanarak en son kararlı veya belirli bir Scarb sürümü olmak üzere bir veya birden fazla Scarb sürümü yükleyebiliriz.
scarb new
kullanarak bir proje oluşturabiliriz- Derlenmiş Sierra kodunu oluşturmak için
scarb build
kullanarak bir proje oluşturabiliriz. - Bir Cairo programını
scarb cairo-run
komutunu kullanarak çalıştırabiliriz.
Scarb kullanmanın ek bir avantajı, hangi işletim sistemi üzerinde çalıştığınıza bakılmaksızın komutların aynı olmasıdır. Bu nedenle, bu noktadan itibaren, Linux ve macOS ile Windows arasındaki belirli talimatları artık sağlamayacağız.
Cairo yolculuğunuzda harika bir başlangıç yaptınız! Cairo kodu okumaya ve yazmaya alışmak için daha sağlam bir program oluşturmak için harika bir zaman!