0% found this document useful (0 votes)
44 views55 pages

Rust Chapter 3

1. The document covers common programming concepts in Rust including variables, data types, arithmetic operations, booleans, characters, constants, tuples, arrays, and scope. 2. It discusses scalar data types like integers and floats of different sizes, as well as defining constants. 3. Compound data types like tuples and arrays are explained along with indexing, destructuring, and nested data structures.

Uploaded by

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

Rust Chapter 3

1. The document covers common programming concepts in Rust including variables, data types, arithmetic operations, booleans, characters, constants, tuples, arrays, and scope. 2. It discusses scalar data types like integers and floats of different sizes, as well as defining constants. 3. Compound data types like tuples and arrays are explained along with indexing, destructuring, and nested data structures.

Uploaded by

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

Chapter 3

Common Programming Concepts


October 20
fn main() {
let x = 5;
let x = x + 1;
5
let x = x * 2; 0x7ffe374185a4 x

println!("The value of x is: {}",


x);
} 0x7ffe374185b0
6
x

0x7ffe374185a8
12
x
fn main() {
let mut age = 22;
age = 33;
println!("The value is: {}", 0x7ffe374185a4 22
33
age
age);
}
Shadowing
Data
Data Types
Scalar
u unsigned ( no - sign)
i signed ( can have - sign)
8 bit = 1 byte
16 bit = 2 byte
32 bit = 4 byte
64 bit = 8 byte
8 bit = 1 byte
fn main() {
let age:u8 = 22;
}

0 0 0 1 0 1 1 0
16 bit = 2 byte
fn main() {
let salary:u16 = 22529;
}

0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1
i8 Range -(2 )
n - 1 to 2n - 1 -1
-(27) to
27 -1
u8 Range 0 to 2n -1
0 to 28 - 1
0 to 255
32 bit = 4 byte
fn main() {
let profit:u32 = 1250255;
}

0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1

0 0 0 1 0 0 1 1 1 1 0 0 1 1 1 1
64 bit = 8 byte
fn main() {
let sale:u64 =
98765425698562;
}
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 1 0 1 1 0 0 1 1

1 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1

0 1 1 0 0 1 1 1 0 0 0 0 0 0 1 0
fn main() {

.
let sale:f32 = 54582 36523;

.
let sale:f64 = 8954569856 36523;
}
{

.
let height = 155 36;
let age = 22;
}
{

.
let height = 155 36; //by default f64 implicit
let age = 22; //by default i32 implicit
}
next
constant
constant
const MAX_MARKS : f32 = 500.00;
fn main () {
let obt_marks = 325.00;
let percentage = obt_marks / MAX_MARKS * 100.0 ;
println! ("{}",percentage)
}
constant after compile

const MAX_MARKS : f32 = 500.00;


fn main () {
let obt_marks = 325.00;
let percentage = obt_marks / 500.00 * 100.0 ;
println! ("{}",percentage)
}
Arithmetic Operations

+
-
*
/
%
boolean
boolean

fn main() {
let t = true;
let f: bool = false;
// with explicit type annotation
}
Character
Character
fn main() {
let data2 = 'z';
let data1 = 'ℤ';
let heart_eyed_cat = '😻';
}
fn main ()
{
let emoji = '\u{1F633}';
println!("{}", emoji);
}

https://www.quackit.com/character_sets/emoji/emoji_v3.0/
unicode_emoji_v3.0_characters_all.cfm

https://apps.timwhitlock.info/emoji/tables/unicode
index value

Compound
0 PIAIC
1 3000
2 IOT

Tuple 3
4
Quarte1
400
5 12.15
index value
Tuple 0 PIAIC
1 3000
2 IOT
3 Quarte1
4 400
fn main() {
5
let data = (“PIAIC”,3000,“IOT”,“Quarter1”,400,12.15); 12.15
println!("Tuple is: {:?}", data);
println!("Index 3 is: {}", data.3);
}
index value
Tuple 0 PIAIC
1 3000
Destructure 2 67.8
fn main() {
let data = ("PIAIC", 3000, 67.8);
let (org, fee, percent) = data;
println!("The fee is y is: {}", fee);
let org1 = data.0;
println!("The Organization is: {}", org1);
}
index value
Compound Array 0 PIAIC
index value index value
1 IOT
0 88.99 0 75 2 AIC
1 50.77 1 62 3 CNC
2 76.89 2 85 4 BCC
3 66.89 3 90 5 SDN
4 99.29 4 69
5 88.29 5 80
Array Destructure
fn main() {
let a = [1, 2, 3, 4, 5];

let first = a[0];


let second = a[1];
}
{

}
{

}
{
{

}
}
Scope
On Oct 27, we will learn Loops
and Functions

You might also like