πŸ“š
Open Polkadot Bootcamp
  • πŸ“š About the Bootcamp
    • πŸ“– Additional Resources
    • πŸ‘ Ask For Support
  • πŸ“– Curriculum
  • πŸ“•Rust Programming Language
    • Basic Rust
      • Introduction to Rust
        • πŸ§‘β€πŸ’» Excercises
      • Common Programming Concepts
        • πŸ§‘β€πŸ’» Excercises
      • Program Life Cycle
        • πŸ§‘β€πŸ’» Excercises
      • Ownership & Borrow Checker
      • Common Data Structures
    • Advanced Rust
      • Generic types, Trait extension and Advanced types
      • Lifetime Notation
      • Smart pointers & Macros
      • Common design patterns in Rust
      • Package management & How to structure your Rust project
      • Overview of the Rust ecosystem
  • πŸ“˜Building a blockchain with Polkadot SDK
    • Polkadot
      • Additional Reads
        • Why do you want to build a blockchain on Polkadot?
        • Understanding the sharded network design of Polkadot
      • Development on Polkadot
    • Polkadot SDK
      • Substrate
        • Create a new blockchain
          • πŸ§‘β€πŸ’» Exercise: Clone the minimal template
          • Understanding the architecture
          • Break down the node architecture
          • Introducing to Pop CLI tool
        • Adding a custom logic to runtime
          • πŸ§‘β€πŸ’» Exercise: Rust State Machine
          • Components of a Pallet
          • Hooks
          • Weights & Benchmarking
          • Extensions
            • Signed Extensions
            • Transaction Extensions
        • Common runtime modules
          • πŸ“•Example: System Pallet
          • πŸ“•Example: Contracts Pallet
          • πŸ“•Example: Assets Pallet
          • πŸ“•Example: Utility Pallet
        • Runtime API and RPC
        • Runtime upgrade
        • Bump Polkadot SDK versions
      • Cumulus
        • Introduction to Cumulus
          • Parachain from scratch
          • πŸ§‘β€πŸ’» Exercise: Build a parachain from scratch
        • Running a local relaychain network
          • Register & reserve a parachain
          • Launch the network & run a collator node
          • Launch the network with Pop CLI
        • Agile Coretime
    • Polkadot Hub
  • πŸ“’Smart Contract Development
    • Introduction
      • Introduction to PolkaVM
      • Getting started with Solidity development
      • Solidity File Structure
      • Contract Structure
    • Basic Solidity
      • Value types
      • Reference Types
      • Mapping Types
      • Simple Storage
    • Advanced Solidity
      • Units
      • Global Variables
      • Expression and Control Structures
      • Advanced Storage
      • Contract Tests
      • Contracts
Powered by GitBook
On this page
  • Preparation
  • Writing the code
  • Compilation
  • Exercises
  1. Rust Programming Language
  2. Basic Rust

Program Life Cycle

PreviousπŸ§‘β€πŸ’» ExcercisesNextπŸ§‘β€πŸ’» Excercises

Last updated 7 months ago

The lifecycle of a Rust program involves several stages from writing the code to running the executable. Here’s a breakdown of each phase:

Before we go to the detailed steps, we must have a full view of components needed for a Rust program:

Preparation

Writing the code

  1. To start a new Rust program, run a command

The main entry point file generated by this command is main.rs

cargo new <app_name>
  1. Or if you build a library, you run a command

Modules and Crates: Code is organized into modules and crates (libraries or binaries). Rust has an ecosystem of reusable libraries (crates) that can be imported to extend functionality.

The main entry point file generated by this command is lib.rs

cargo new --lib <app_name>
  1. Package management file

  1. Understand basic syntax

Here's a quick overview of Rust's basic syntax with examples to help you get started:

  • Variables and Mutability: Variables are immutable by default, meaning you can't change their values unless explicitly marked with mut.

fn main() {
    let x = 5;        // Immutable variable
    let mut y = 10;   // Mutable variable
    y += 5;
    println!("x: {}, y: {}", x, y);
}
  • Data types: We already learnt about data types in the last lesson. Please revise if you forget.

  • Functions: Functions are defined with the fn keyword, and parameters need types.

fn main() {
    greet("Alice");
    let result = add(5, 3);
    println!("Sum: {}", result);
}

fn greet(name: &str) {
    println!("Hello, {}!", name);
}

fn add(a: i32, b: i32) -> i32 {
    a + b   // No semicolon means this is the return value
}
  • Conditionals: if, else if, and else for conditional logic.

fn main() {
    let number = 7;
    if number < 5 {
        println!("Less than 5");
    } else if number == 5 {
        println!("Equal to 5");
    } else {
        println!("Greater than 5");
    }
}
  • Loops: Rust has several looping constructs: loop, while, and for.

fn main() {
    let mut count = 0;

    // Infinite loop
    loop {
        if count >= 3 {
            break;
        }
        println!("Count: {}", count);
        count += 1;
    }

    // While loop
    while count < 5 {
        println!("Count: {}", count);
        count += 1;
    }

    // For loop
    for i in 0..5 {
        println!("i: {}", i);
    }
}

Those are basic syntax that you must know before diving into harder concetps.

Compilation

To compile and run the Rust program, run the command:

cargo run
  • Rust’s rustc compiler translates the source code into machine code in a single pass, focusing on safety and performance.

  • The compilation process includes:

    • Parsing: Rust parses the code to check for syntax correctness.

    • Analysis and Borrow Checker: Rust’s borrow checker enforces memory safety rules by validating ownership, borrowing, and lifetime rules, ensuring memory safety without a garbage collector.

    • Code Generation and Optimization: Rust generates optimized machine code, removing unused variables, inlining functions, and applying other optimizations.

  • Result: Rust produces an intermediate binary in the form of an object file, then links these files to produce an executable file.

Exercises

Make sure you have all installation ready:

After that, you need to install package manager:

And then the Rust compiler:

Cargo.toml and Cargo.lock is the two files that manage the dependencies of the Rust program. To understand how to use these files correctly, follow this guide:

πŸ“•
https://doc.rust-lang.org/book/ch01-01-installation.html
https://doc.rust-lang.org/cargo/
https://doc.rust-lang.org/rustc/
https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Module 1.2 - Program Life Cycle
Logo
Rust Practices with Rustlings - FunctionsOpenGuild Community
Logo