Rise In Logo

Build on Internet Computer with ICP Rust CDK

Creating Smart Contract

Creating Smart Contract

Introduction to Creating a Smart Contract on ICP with Rust

In this video, we walk through the process of creating a basic smart contract using Rust on the Internet Computer Protocol (ICP), introducing core concepts like canister setup, query vs. update functions, and how the frontend communicates with the backend through Candid.

Step 1: Creating a Rust Canister with DFX

Start by creating a new canister project with Rust as the backend:

dfx new example --type rust
cd example

This command sets up the initial project structure for a Rust-based canister. Once inside the project directory, open it using an IDE like Visual Studio Code for easier navigation and editing.

Step 2: Understanding the Project Structure

Within the project, you'll find the following:

  • src/: Contains source code
  • example_backend/: The Rust backend canister
  • example_frontend/: Placeholder for frontend code (not used in this tutorial)
  • dfx.json: Configuration file for your DFX project
  • Cargo.toml: Rust dependency configuration
  • package.json, .env, .gitignore, and others

Inside example_backend, another Cargo.toml is present because it represents a separate Rust project. The lib.rs file in the src directory is where we define our smart contract logic.

Step 3: Writing a Basic Query Function

In lib.rs, a simple query function is defined using the ic_cdk dependency:

#[ic_cdk::query]
fn greet(name: String) -> String {
    format!("Hello, {}!", name)
}
  • Query functions are read-only and cost no gas.
  • Update functions modify blockchain state and incur gas fees.

Step 4: Communicating with the Frontend via Candid

To enable frontend interaction, you use a Candid interface file. The .did file defines how external applications can communicate with the canister:

service : {
  "greet": (text) -> (text) query;
}
  • text replaces String in Candid syntax.
  • The query keyword denotes this is a read-only operation.
  • Update functions are defined similarly but without the query tag.

Summary and Next Steps

In this lesson, we:

  • Created a new Rust-based canister using DFX
  • Examined the file structure of an ICP project
  • Wrote a simple greet query function
  • Defined its Candid interface for frontend integration

Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!

Disclaimer: The information /programs / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.