0% found this document useful (0 votes)
6 views7 pages

Programming For AI Lab

This lab manual provides a comprehensive introduction to Rust programming, covering installation, setup, and practical exercises. It includes topics such as variables, ownership, error handling, and file I/O, along with debugging tips in Visual Studio Code. The manual is designed for users transitioning from other programming languages and emphasizes Rust's performance and safety features.

Uploaded by

Muhammad Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Programming For AI Lab

This lab manual provides a comprehensive introduction to Rust programming, covering installation, setup, and practical exercises. It includes topics such as variables, ownership, error handling, and file I/O, along with debugging tips in Visual Studio Code. The manual is designed for users transitioning from other programming languages and emphasizes Rust's performance and safety features.

Uploaded by

Muhammad Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual 7

Rust Programming

Contents

Introduction to Rust Programming 2

1 Lab Setup 2
1.1 Install Rust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Install VS Code and Required Extensions . . . . . . . . . . . . . . . . . . . . 3

2 Exercise 1: Hello, World! 3

3 Exercise 2: Variables & Mutability 3

4 Exercise 3: Ownership & Borrowing 3

5 Exercise 4: Structs & Methods 4

6 Exercise 5: Error Handling 4

7 Exercise 6: Functions & Control Flow 4

8 Exercise 7: Arrays & Loops 5

9 Exercise 8: Enums & Pattern Matching 5

10 Exercise 9: The Option Type 6

11 Exercise 10: Basic File I/O 6

12 Debugging in VS Code 6

13 Next Steps 7

14 Troubleshooting 7

1
Introduction to Rust Programming
Rust is a modern, statically-typed systems programming language known for combining
performance and safety. It is designed to write fast, memory-efficient programs without
sacrificing reliability or concurrency.
Rust is widely used to build:

• System software like operating systems and device drivers

• Web servers and backend APIs

• Embedded applications for IoT devices

• CLI tools and automation scripts

• Game engines and high-performance simulations

What makes Rust stand out:

• Memory safety without garbage collection

• Zero-cost abstractions with high-level ergonomics

• Ownership and borrowing to eliminate data races

• Fearless concurrency with built-in thread safety

• Great tooling via cargo, rustfmt, and clippy

Whether you’re coming from C/C++, Python, or Java, learning Rust helps you build
more reliable and efficient software. This lab manual introduces Rust step-by-step, using
practical examples and the Visual Studio Code IDE on Windows.
Official Website: https://www.rust-lang.org
Documentation: https://doc.rust-lang.org/book/

Lab Setup
Install Rust
Open CMD as Administrator and run:
1 winget install -- id Rustlang . Rustup

Follow the prompts (press 1 for default installation).


Verify Installation:
1 rustc -- version
2 cargo -- version

2
Install VS Code and Required Extensions
Download Visual Studio Code: https://code.visualstudio.com/
Install these extensions from the Extensions tab:
• Rust Analyzer – Code completion and diagnostics
• CodeLLDB – Debugging support
• Better TOML – Syntax highlighting for Cargo.toml

Exercise 1: Hello, World!


1 fn main () {
2 println !(" Hello , World !") ;
3 }
Steps:
1 cargo new hello_world
2 cd hello_world
3 cargo run
Expected Output:
Hello, World!

Exercise 2: Variables & Mutability


1 fn main () {
2 let x = 5;
3 let mut y = 10;
4 y += 1;
5 println !(" x = {} , y = {}" , x , y ) ;
6 }
Output:
x = 5, y = 11

Exercise 3: Ownership & Borrowing


1 fn main () {
2 let s1 = String :: from (" hello ") ;
3 let s2 = s1 . clone () ;
4 println !(" s1 = {} , s2 = {}" , s1 , s2 ) ;
5 }
Output:
s1 = hello, s2 = hello

3
Exercise 4: Structs & Methods
1 struct Rectangle {
2 width : u32 ,
3 height : u32 ,
4 }
5
6 impl Rectangle {
7 fn area (& self ) -> u32 {
8 self . width * self . height
9 }
10 }
11
12 fn main () {
13 let rect = Rectangle { width : 30 , height : 50 };
14 println !(" Area : {}" , rect . area () ) ;
15 }

Output:

Area: 1500

Exercise 5: Error Handling


1 fn divide ( numerator : f64 , denominator : f64 ) -> Result < f64 , String > {
2 if denominator == 0.0 {
3 Err ( String :: from (" Cannot divide by zero !") )
4 } else {
5 Ok ( numerator / denominator )
6 }
7 }
8
9 fn main () {
10 match divide (10.0 , 0.0) {
11 Ok ( result ) = > println !(" Result : {}" , result ) ,
12 Err ( e ) = > println !(" Error : {}" , e ) ,
13 }
14 }

Output:

Error: Cannot divide by zero!

Exercise 6: Functions & Control Flow


1 fn check_number ( num : i32 ) {
2 if num > 0 {
3 println !(" Positive ") ;
4 } else if num < 0 {

4
5 println !(" Negative ") ;
6 } else {
7 println !(" Zero ") ;
8 }
9 }
10
11 fn main () {
12 check_number ( -5) ;
13 }

Output:
Negative

Exercise 7: Arrays & Loops


1 fn main () {
2 let numbers = [10 , 20 , 30 , 40 , 50];
3 for num in numbers . iter () {
4 println !(" Number : {}" , num ) ;
5 }
6 }

Output:
Number: 10
Number: 20
...
Number: 50

Exercise 8: Enums & Pattern Matching


1 enum TrafficLight {
2 Red ,
3 Yellow ,
4 Green ,
5 }
6
7 fn signal_duration ( light : TrafficLight ) -> u32 {
8 match light {
9 TrafficLight :: Red = > 30 ,
10 TrafficLight :: Yellow = > 5 ,
11 TrafficLight :: Green = > 45 ,
12 }
13 }
14

15 fn main () {
16 let light = TrafficLight :: Yellow ;
17 println !(" Duration : {} seconds " , signal_duration ( light ) ) ;
18 }

5
Output:

Duration: 5 seconds

Exercise 9: The Option Type


1 fn find_index ( arr : &[ i32 ] , target : i32 ) -> Option < usize > {
2 for ( index , & num ) in arr . iter () . enumerate () {
3 if num == target {
4 return Some ( index ) ;
5 }
6 }
7 None
8 }
9

10 fn main () {
11 let numbers = [3 , 7 , 2 , 9];
12 match find_index (& numbers , 2) {
13 Some ( i ) = > println !(" Found at index {}" , i ) ,
14 None = > println !(" Not found ") ,
15 }
16 }

Output:

Found at index 2

Exercise 10: Basic File I/O


1 use std :: fs :: File ;
2 use std :: io :: prelude ::*;
3
4 fn main () -> std :: io :: Result <() > {
5 let mut file = File :: create (" hello . txt ") ?;
6 file . write_all ( b " Hello , Rust file !") ?;
7 Ok (() )
8 }

Check: Ensure hello.txt is created in your project directory.

Debugging in VS Code
• Set breakpoints in the left gutter.

• Press F5 to start debugging.

• Use the debug toolbar to step through your code.

6
Next Steps
• Try online at the Rust Playground: https://play.rust-lang.org

• Read the official Rust Book: https://doc.rust-lang.org/book/

• Explore more: https://www.rust-lang.org/learn

Troubleshooting
• Rust Analyzer not working? Reload VS Code or run: rustup update

• CMD doesn’t recognize cargo? Restart the terminal or check your system PATH
variable.

You might also like