0% found this document useful (0 votes)
18 views

COMP3007 - Modern - Programming - Languages - Week 3

Modern Programming Languages Week 3

Uploaded by

ytuncel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

COMP3007 - Modern - Programming - Languages - Week 3

Modern Programming Languages Week 3

Uploaded by

ytuncel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COMP3007 - Modern Programming Languages

Week 3: Rust - Introduction and Basic Syntax

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel

Department of Computer Engineering

Fall 2024-2025

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 1 / 12
Outline

1 Introduction to Rust

2 Setting Up Rust Environment

3 Basic Syntax

4 Functions

5 Comments and Documentation

6 Rust Playground

7 Conclusion

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 2 / 12
Installing Rust

Use rustup: https://rustup.rs/


Installs Rust compiler (rustc), package manager (cargo), and standard
library
Available on Windows, macOS, and Linux
Commands to check installation:
r u s t c −−v e r s i o n
c a r g o −−v e r s i o n

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 3 / 12
Hello, World!

fn main () {
println !("Hello , World !");
}

fn: Function declaration


main(): Entry point of the program
println!: Macro for printing to console

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 4 / 12
Variables and Mutability

let x = 5; // immutable by default


let mut y = 10; // mutable variable
y = 15; // OK
x = 6; // Error : cannot assign twice to immutable variable

Variables are immutable by default


Use mut keyword for mutable variables
Type inference is used, but can be explicitly specified

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 5 / 12
Control Flow

let number = 6;

if number % 4 == 0 {
println !(" number is divisible by 4");
} else if number % 3 == 0 {
println !(" number is divisible by 3");
} else {
println !(" number is not divisible by 4 or 3");
}

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 6 / 12
Loops

// loop
loop {
println !("This will print forever ");
}

// while loop
let mut number = 3;
while number != 0 {
println !("{}!", number );
number -= 1;
}

// for loop
for number in (1..4). rev () {
println !("{}!", number );
}

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 7 / 12
Function Syntax

fn add(x: i32 , y: i32) -> i32 {


x + y // Implicit return (no semicolon )
}

fn main () {
let result = add (5, 7);
println !("The sum is: {}", result );
}

Function parameters must have type annotations


Return type is specified after ->
Last expression in a function is implicitly returned

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 8 / 12
Comments and Documentation

// This is a single -line comment

/*
This is a
multi -line comment
*/

/// This is a documentation comment


/// It can be used to generate documentation
fn documented_function () {
// Function body
}

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 9 / 12
Rust Playground

Online Rust compiler and execution environment


Great for quick experiments and sharing code
URL: https://play.rust-lang.org/
Features:
Compile and run Rust code
Share code snippets
Choose different versions of Rust
Access to popular crates

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 10 / 12
Conclusion

Recap of Rust’s basic syntax and concepts


Importance of practicing these fundamentals
Preview of next week’s topic: Ownership and Borrowing

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 11 / 12
Thank You! Any Questions?

Dr. Öğr. Üyesi Yusuf Kürşat Tuncel (Department


COMP3007
of Computer
- Modern
Engineering)
Programming Languages Fall 2024-2025 12 / 12

You might also like