0% found this document useful (0 votes)
15 views5 pages

Chapter 2

The document discusses Rust programming language concepts including variables, data types, functions, control flow, and loops. Variables in Rust are immutable by default but can be made mutable. Data types include scalar types like integers, floats, booleans, and characters, as well as compound types like tuples and arrays. Functions are declared with fn and can return values. Control flow is handled with if/else statements and loops include loop, while, and for.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Chapter 2

The document discusses Rust programming language concepts including variables, data types, functions, control flow, and loops. Variables in Rust are immutable by default but can be made mutable. Data types include scalar types like integers, floats, booleans, and characters, as well as compound types like tuples and arrays. Functions are declared with fn and can return values. Control flow is handled with if/else statements and loops include loop, while, and for.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Variables

* immutable by default
- but we can make the mutable if we want, by mut keyword

immutable: once a variable is bound to a value, we can't change that value


- reduces possible bugs in the program

Constants
- always immutable, can't use mut
- const keyword, type must be declared
- can be declared in global scope
- can only be set up to a const expression, not a value computed at runtime
- valid for entire program

Shadowing
- declaring a new variable with the same name as the previous one
- goes on until the variable itself is shadowed / the scope ends
- allows changing type (can't with mut)

{} can be used freely to create a scope

-------------------------------------------------------

Data Types

Rust is statically typed:


- it must know the type of each variable at compile time
- eg: Java, C, Rust

dynamically typed language


- it must know the type of each variable at run time
- eg: Python, JavaScript

* compiler det.s type by values


- in multiple values case: type must be declared

Scalar Types
- represent a single value

4 primary scalar types


1. Integers 2. Floating-point numbers 3. Booleans 4. Characters

Integer:
- number without fractional part
u: unsigned
i: signed (based on Two’s Complement encoding)
i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize (arch)

default: i32

Integer literals
- decimal: 98_222
- hex: 0xff
- octal: 0o77
- binary: 0b1111_0000
- byte (u8 only): b’A’
integer overflow
- debug mode: panic
- release mode: two’s complement wrapping

Floating-point numbers
- numbers with fractional part
f32, f64
default: f64

Booleans
- true, false
- 1 byte size

Chars
- single quotes ‘’
- 4 bytes in size, represents Unicode scalar value

Compound Types
- group multiple values into one type

2 primitive compound types


1. tuples
2. arrays

Tuples
- groups multiple values of various types into one compound type
- have fixed size (can’t grow or shrink)
- each element has a type

declaration: let tup : (type1, …, typen) = (t1, t2, …, tn)


- comma-seperated list of values inside parantheses

to destructure the tuple: pattern-matching


let (x, y, z) = tup

accessing elements: tup.idx

unit: tuple without any values


- () value, has type ()
- represent an empty value or empty return type
- expressions implicitly return unit value if they don’t return any other value

Arrays

- group multiple values of same type into one compound type


- have fixed size
- allocated on stack

declaration: comma-seperated list of values inside square brackets


- defining type: let a: [type; length] = …
- initializing each element with same value: let a = [val; length];

vector: dynamic size


accessing elements: a[idx]

invalid index access → “index out of bounds” error

------------------------------------------------------------

Functions

fn: declare new functions


main()

snake_case convention for function names and variables

function declaration: order does not matter as long as defined in a scope seen by
caller

parameter: special variables that are part of function’s signature


- types are required

Statements and Expressions

Statements: instructions that perform an action and do not return a value (return
())
Expressions: instructions that evaluate to a resultant value

statement examples
- declaring functions, declaring variables (different from C)
* can’t write x = y = 6

expression examples
- 5 + 6
- 6
- calling functions: returns a value or ()
- calling macros: returns ()
- a new scope created with {}: evaluates to last expression in it;

* expressions do not include ending ; → turns into statements

* expressions can be a part of statements


- reverse not possible

Return values
- must declare type with arrow (→)
* fn main() → type
- consist of expressions
- can use “return” to return early

Comments

comments: ignored by compiler, but read by people


Rust comments: //

Control Flow

if: conditional branching


* conditional: must be bool

using if in a let statement

- if is an expression: value equals the return value of which block it executes


→ can be used in let statements

let var = if condition {exp1} else {exp2}

* types of values from exp1 and exp2 must be the same.


- Rust is statically-typed: it needs to know the type of each variable at compile
time

Loops

Rust has 3 types of loops


1. loop
2. while
3. for

loop: sets up infinite loop


- break: to exit from innermost loop
- continue: to skip to the next iteration of the innermost loop

Returning values from loops


- loops are also expressions
- break value; → value will be passed to outside the loop

Loop labels
- loops can be attached with labels which can be used with break and continues
→ break and continue will apply to specified loop instead of innermost loop.

‘label: loop {}
break ‘label;
continue ‘label;

While: conditional loop

while condition { }

For: iterating collections

iterating collections with while


- error-prone
- additional cost of condition check

for element in a { }

* for can also be used in conditional loops


- by using ranges

for number in (1..4).rev() { }


- (1..4) denotes range
- rev() reverses the range

You might also like