পাঠ ১.২

Hello, World!

Hello, World!

Rust install করা, প্রথম program লেখা, এবং cargo দিয়ে compile ও run করানো।

Rust কী এবং কেন?

Rust হলো একটি systems programming language — মানে এটা দিয়ে operating system, browser engine, embedded device, কিংবা high-performance server লেখা যায়। Rust-এর মূল আকর্ষণ হলো এর ownership system, যেটি memory safety নিশ্চিত করে garbage collector ছাড়াই।

C++ থেকে আসলে তুমি দেখবে Rust অনেক চেনা — pointer, reference, generic, trait — কিন্তু compile-time-এ এত strict checking যে data race বা use-after-free-এর মতো bug runtime-এ পৌঁছায়ই না।

Rust install করা

সবচেয়ে সহজ উপায় হলো rustup। তোমার terminal-এ এই command চালাও:

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

Install শেষ হলে নতুন একটা terminal খুলে check করো — rustc হলো Rust compiler আর cargo হলো Rust-এর build tool ও package manager:

terminalbash
rustc --version
cargo --version

প্রথম project

নতুন একটা project বানাতে cargo new ব্যবহার করো:

terminalbash
cargo new hello_world
cd hello_world

এই command একটা folder তৈরি করবে যেখানে দুটো জিনিস আছে — Cargo.toml (project-এর metadata ও dependency list) আর src/main.rs (তোমার source code)। ভিতরে গিয়ে src/main.rs খুলে দেখো:

src/main.rsrust
fn main() {
    println!("Hello, world!");
}

Code-টা ভেঙে বুঝি

fn keyword দিয়ে আমরা একটা function declare করি। এখানে function-এর নাম main — এটা একটা special function, কারণ Rust program সবসময় এখান থেকেই execute শুরু হয়।

println!-এর শেষে যে ! দেখছ, এটা বোঝায় এটি একটা macro, function না। macro হলো compile-time-এ চালানো code-generation tool। আপাতত এটুকু জানাই যথেষ্ট — পরের পাঠে আমরা macro নিয়ে বিস্তারিত আলোচনা করব।

Build ও run

cargo run দিলেই compile হবে এবং program execute হবে:

terminalbash
$ cargo run
   Compiling hello_world v0.1.0 (/path/to/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 1.23s
     Running `target/debug/hello_world`
Hello, world!

শুধু compile করতে চাইলে cargo build, আর production-এর জন্য optimized binary বানাতে cargo build --release। Release build অনেক fast হয়, কিন্তু compile সময় বেশি লাগে।

এই পাঠ থেকে যা শিখলে

  • rustup দিয়ে Rust toolchain install করা
  • cargo new দিয়ে নতুন project তৈরি করা
  • fn main() হলো program-এর entry point
  • println! একটা macro — তাই শেষে !
  • cargo run, cargo build, cargo build --release