Differences

This shows you the differences between two versions of the page.

Link to this comparison view

saiot:laboratoare:01 [2020/10/07 14:00]
127.0.0.1 external edit
saiot:laboratoare:01 [2020/10/21 23:31] (current)
alexandru.radovici
Line 1: Line 1:
-===== Laboratorul 01. =====+===== Lab 1- Introduction to Rust =====
  
 +===== Workpoint 1 =====
 +<code rust>
 +#​![allow(dead_code)]
 +fn main() {
 +    let name = "​Ruster";​
 +    println! ("​Hello Rusters, I am {} and Rust is awseome",​ name);
 +}
 +</​code>​
 +
 +===== Workpoint 2 =====
 +<code rust>
 +#​![allow(dead_code)]
 +fn main() {
 +    let a = 10;
 +    let b = 20;
 +    // print debug information for the a + b expression
 +    dbg! (a+b);
 +}
 +</​code>​
 +
 +===== Workpoint 3 =====
 +<code rust>
 +#​![allow(dead_code)]
 +fn main() {
 +    let a = 10;
 +    let b = 100;
 +    let max = if a > b { a } else { b };
 +    ​
 +    println! ("The maximum {}", max);
 +}
 +</​code>​
 +
 +===== Workpoint 4 =====
 +<code rust>
 +#​![allow(dead_code)]
 +fn main() {
 +    let n = 10;
 +    ​
 +    let t = match n % 2 {
 +      0 => "​even",​
 +      _ => "​odd"​
 +    };
 +    ​
 +    println! ("{} is {}", n, t);
 +}
 +</​code>​
 +
 +===== Workpoint 5 =====
 +<code rust>
 +#​![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));​
 +}
 +</​code>​
 +
 +===== Workpoint 6 =====
 +<code rust>
 +#​![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);
 +}
 +</​code>​
 +
 +===== Workpoint 7 =====
 +==== Workpoint 7.1 ====
 +<code rust>
 +#​![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);
 +}
 +</​code>​
 +
 +==== Workpoint 7.2 ====
 +<code rust>
 +#​![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);
 +}
 +</​code>​
 +
 +===== Workpoint 8 =====
 +<code rust>
 +#​![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);
 +}
 +</​code>​
 +
 +===== Workpoint 9 =====
 +
 +Solution: Catalin Gheorghe
 +
 +<code rust>
 +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
 +}
 +</​code>​
  
saiot/laboratoare/01.1602068432.txt.gz ยท Last modified: 2020/10/21 23:31 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0