## Workpoint 1

#![allow(dead_code)]
fn main() {
let name = "Ruster";
println! ("Hello Rusters, I am {} and Rust is awseome", name);
}

## Workpoint 2

#![allow(dead_code)]
fn main() {
let a = 10;
let b = 20;
// print debug information for the a + b expression
dbg! (a+b);
}

## Workpoint 3

#![allow(dead_code)]
fn main() {
let a = 10;
let b = 100;
let max = if a > b { a } else { b };

println! ("The maximum {}", max);
}

## Workpoint 4

#![allow(dead_code)]
fn main() {
let n = 10;

let t = match n % 2 {
0 => "even",
_ => "odd"
};

println! ("{} is {}", n, t);
}

## Workpoint 5

#![allow(dead_code)]
fn factorial (n: i32) -> u32 {
match n {
0 | 1 => 1,
n if n > 0 => n as u32 * factorial (n-1),
_ => panic! ("not possible")
}
}

fn main () {
println! ("10!: {} 0!: {} -10: {}", factorial(10), factorial(0), factorial(-10));
}

## Workpoint 6

#![allow(dead_code)]
fn main() {
let v = [1,2,4,5,23,54,6,7,34,6,6,3];
let mut max = v[0];

for x in &v {
if *x > max {
max = *x;
}
}

println! ("The maximum {}", max);
}

## Workpoint 7

### Workpoint 7.1

#![allow(dead_code)]

fn maximum (a: u32, b: u32) -> u32 {
if a > b {
a
}
else
{
b
}
}

fn main() {
let a = 10;
let b = 100;
let max = maximum (a, b);

println! ("The maximum {}", max);
}

### Workpoint 7.2

#![allow(dead_code)]

fn maximum (v: &[u32]) -> u32 {
let mut max = v[0];
for x in v {
if *x > max {
max = *x;
}
}
max
}

fn main() {
let v = [1,2,4,5,23,54,6,7,34,6,6,3];
let max = maximum (&v);

println! ("The maximum {}", max);
}

## Workpoint 8

#![allow(dead_code)]
fn main() {
let v = [1,2,4,5,23,54,6,7,34,6,6,3];
let max = v.iter().fold (v[0], |max, x| {
if *x > max
{
*x

} else {
max
}
});

println! ("The maximum {}", max);
}

## Workpoint 9

Solution: Catalin Gheorghe

fn factorial (n: i32) -> Option<u32> {
if n < 0 {
return None
}
match factorial(n-1) {
Some(value) => Some(value * (n as u32)),
None => Some(1)
}
}
fn map(option: Option<u32>) -> String {
match option {
Some(value) => value.to_string(),
None => "not possible".to_string()
}
}
fn main () {
println! ("10!: {}, 0!: {}, -10!: {}", map(factorial(10)), map(factorial(0)), map(factorial(-10)));
// prints 10!: 3628800, 0!: 0, 10!: not possible
}
saiot/laboratoare/01.txt ยท Last modified: 2020/10/21 23:31 by alexandru.radovici