Differences

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

Link to this comparison view

saiot:laboratoare:02 [2020/10/21 23:05]
alexandru.radovici
saiot:laboratoare:02 [2020/10/21 23:16] (current)
alexandru.radovici
Line 1: Line 1:
 ====== Lab 2 - Rust Traits and Secure References ====== ====== Lab 2 - Rust Traits and Secure References ======
 +
 +===== Workpoint 1 =====
 +<code rust>
 +#​![allow(dead_code)]
 +fn main() {
 +    struct Color(i32, i32, i32);
 +    ​
 +    let c = Color(0, 255, 255);
 +    println!("​({},​ {}, {})", c.0, c.1, c.2);
 +}
 +</​code>​
 +
 +===== Workpoint 2 =====
 +<code rust>
 +#​![allow(dead_code)]
 +struct Person {
 +    firstname: String,
 +    lastname: String,
 +    age: usize,
 +}
 +
 +impl Person {
 +    pub fn new(firstname:​ String, lastname: String, age: usize) -> Person {
 +        Person { firstname: firstname, lastname: lastname, age:age }
 +    }
 +  ​
 +    pub fn set_firstname (&mut self, firstname: String) {
 +        self.firstname = firstname;
 +    }
 +    ​
 +    pub fn set_lastname (&mut self, lastname: String) {
 +        self.lastname = lastname;
 +    }
 +}
 +
 +fn main () {
 +    let mut p = Person::new ("​The"​.to_string(),​ String::​from("​Name"​),​ 9);
 +    p.set_firstname ("​New"​.to_string());​
 +    println! ("{} {} is now {}", p.firstname,​ p.lastname, p.age);
 +}
 +</​code>​
 +
  
 ===== Workpoint 3 ===== ===== Workpoint 3 =====
 +
 +Solution: Ioana CIUPITU
  
 <code rust> <code rust>
Line 61: Line 105:
   println! ("{} is a {}", p.get_name (), p.get_job());​   println! ("{} is a {}", p.get_name (), p.get_job());​
   println! ("{} is a {}", s.get_name (), s.get_job());​   println! ("{} is a {}", s.get_name (), s.get_job());​
 +}
 +</​code>​
 +
 +===== Workpoint 4 =====
 +
 +Solution: Alexandru HOGEA
 +
 +<code rust>
 +#​![allow(dead_code)]
 +
 +use std::fmt;
 +use std::​ops::​*;​
 +use std::​cmp::​*;​
 +
 +#​[derive(Copy,​ Clone)] // used for impl Add, Sub and Mul
 +struct Complex {
 +    r: f64,
 +    i: f64
 +}
 +
 +impl Complex {
 +    fn new(r: f64, i: f64) -> Complex {
 +        Complex { r, i }
 +    }
 +}
 +impl fmt::​Display for Complex {
 +    fn fmt(&​self,​ f: &mut fmt::​Formatter<'​_>​) -> fmt::Result {
 +        if self.r == 0.0 {
 +            write!(f, "​({}i)",​ self.i)
 +        } else if self.i > 0.0 {
 +            write!(f, "​({}+{}i)",​ self.r, self.i)
 +        } else if self.i == 0.0 {
 +            write!(f, "​({})",​ self.r)
 +        } else {
 +            write!(f, "​({}{}i)",​ self.r, self.i)
 +        }
 +    }
 +}
 +
 +impl Mul for Complex {
 +    type Output = Self;
 +    //    z. a  = x. a  * y. a  - x. b  * y. b ;
 +    //    z. b  = x. a  * y. b  + x. b  * y. a ;
 +    fn mul(self, rhs: Self) -> Self {
 +        let r = self.r * rhs.r - self.i * rhs.i;
 +        let i = self.r * rhs.i + self.i * rhs.r;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl Add for Complex {
 +    type Output = Self;
 +    fn add(self, rhs: Self) -> Self {
 +        let r = self.r + rhs.r;
 +        let i = self.i + rhs.i;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl Sub for Complex {
 +    type Output = Self;
 +    fn sub(self, rhs: Self) -> Self {
 +        let r = self.r - rhs.r;
 +        let i = self.i - rhs.i;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +fn main () {
 +    let n1 = Complex::​new (2.0, 3.0);
 +    let n2 = Complex::​new (-2.0, 3.0);
 +    let n3 = Complex::​new (2.0, -3.0);
 +    let n4 = Complex::​new (3.0, 0.0);
 +    let n5 = Complex::​new (0.0, 3.0);
 +
 +    println! ("The number is {}", n1); // prints 2+3i
 +    println! ("The number is {}", n2); // prints -2+3i
 +    println! ("The number is {}", n3); // prints 2-3i
 +    println! ("The number is {}", n4); // prints 3
 +    println! ("The number is {}", n5); // prints 3i
 +
 +    println! ("The number is {}", n1-n1); // prints 0
 +    println! ("The number is {}", n1+n2); ​
 +    println! ("The number is {}", n1-n2);
 +    println! ("The number is {}", n1*n2);
 +}
 +</​code>​
 +
 +===== Workpoint 5 =====
 +
 +Solution: Alexandru HOGEA
 +
 +<code rust>
 +#​![allow(dead_code)]
 +
 +use std::fmt;
 +use std::​ops::​*;​
 +use std::​cmp::​*;​
 +
 +#​[derive(Copy,​ Clone)] // used for impl Add, Sub and Mul
 +struct Complex {
 +    r: f64,
 +    i: f64
 +}
 +
 +impl Complex {
 +    fn new(r: f64, i: f64) -> Complex {
 +        Complex { r, i }
 +    }
 +}
 +impl fmt::​Display for Complex {
 +    fn fmt(&​self,​ f: &mut fmt::​Formatter<'​_>​) -> fmt::Result {
 +        if self.r == 0.0 {
 +            write!(f, "​({}i)",​ self.i)
 +        } else if self.i > 0.0 {
 +            write!(f, "​({}+{}i)",​ self.r, self.i)
 +        } else if self.i == 0.0 {
 +            write!(f, "​({})",​ self.r)
 +        } else {
 +            write!(f, "​({}{}i)",​ self.r, self.i)
 +        }
 +    }
 +}
 +
 +impl Mul for Complex {
 +    type Output = Self;
 +    //    z. a  = x. a  * y. a  - x. b  * y. b ;
 +    //    z. b  = x. a  * y. b  + x. b  * y. a ;
 +    fn mul(self, rhs: Self) -> Self {
 +        let r = self.r * rhs.r - self.i * rhs.i;
 +        let i = self.r * rhs.i + self.i * rhs.r;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl Add for Complex {
 +    type Output = Self;
 +    fn add(self, rhs: Self) -> Self {
 +        let r = self.r + rhs.r;
 +        let i = self.i + rhs.i;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl Sub for Complex {
 +    type Output = Self;
 +    fn sub(self, rhs: Self) -> Self {
 +        let r = self.r - rhs.r;
 +        let i = self.i - rhs.i;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl PartialEq for Complex {
 +    fn eq(&​self,​ other: &Self) -> bool {
 +        self.r == other.r && self.i == other.i
 +    }
 +}
 +
 +fn main () {
 +    let n1 = Complex::​new (2.0, 3.0);
 +    let n2 = Complex::​new (-2.0, 3.0);
 +    let n3 = Complex::​new (2.0, -3.0);
 +    let n4 = Complex::​new (3.0, 0.0);
 +    let n5 = Complex::​new (0.0, 3.0);
 +
 +    println! ("The number is {}", n1); // prints 2+3i
 +    println! ("The number is {}", n2); // prints -2+3i
 +    println! ("The number is {}", n3); // prints 2-3i
 +    println! ("The number is {}", n4); // prints 3
 +    println! ("The number is {}", n5); // prints 3i
 +
 +    println! ("The number is {}", n1-n1); // prints 0
 +    println! ("The number is {}", n1+n2); ​
 +    println! ("The number is {}", n1-n2);
 +    println! ("The number is {}", n1*n2);
 +    println! ("The numbers {} and {} are {}", n1, n2, if n1==n2 { "​equal"​} else {"not equal"​});​
 +}
 +</​code>​
 +
 +===== Workpoint 6 =====
 +
 +Solution: Alexandru HOGEA
 +
 +<code rust>
 +#​![allow(dead_code)]
 +
 +use std::fmt;
 +use std::​ops::​*;​
 +use std::​cmp::​*;​
 +
 +#​[derive(Copy,​ Clone)] // used for impl Add, Sub and Mul
 +struct Complex {
 +    r: f64,
 +    i: f64
 +}
 +
 +impl Complex {
 +    fn new(r: f64, i: f64) -> Complex {
 +        Complex { r, i }
 +    }
 +}
 +impl fmt::​Display for Complex {
 +    fn fmt(&​self,​ f: &mut fmt::​Formatter<'​_>​) -> fmt::Result {
 +        if self.r == 0.0 {
 +            write!(f, "​({}i)",​ self.i)
 +        } else if self.i > 0.0 {
 +            write!(f, "​({}+{}i)",​ self.r, self.i)
 +        } else if self.i == 0.0 {
 +            write!(f, "​({})",​ self.r)
 +        } else {
 +            write!(f, "​({}{}i)",​ self.r, self.i)
 +        }
 +    }
 +}
 +
 +impl Mul for Complex {
 +    type Output = Self;
 +    //    z. a  = x. a  * y. a  - x. b  * y. b ;
 +    //    z. b  = x. a  * y. b  + x. b  * y. a ;
 +    fn mul(self, rhs: Self) -> Self {
 +        let r = self.r * rhs.r - self.i * rhs.i;
 +        let i = self.r * rhs.i + self.i * rhs.r;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl Add for Complex {
 +    type Output = Self;
 +    fn add(self, rhs: Self) -> Self {
 +        let r = self.r + rhs.r;
 +        let i = self.i + rhs.i;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl Sub for Complex {
 +    type Output = Self;
 +    fn sub(self, rhs: Self) -> Self {
 +        let r = self.r - rhs.r;
 +        let i = self.i - rhs.i;
 +        Complex::​new(r,​ i)
 +    }
 +}
 +
 +impl PartialEq for Complex {
 +    fn eq(&​self,​ other: &Self) -> bool {
 +        self.r == other.r && self.i == other.i
 +    }
 +}
 +
 +impl PartialOrd for Complex {
 +    fn partial_cmp(&​self,​ other: &​Complex) -> Option<​Ordering>​ {
 +        let radical_self = (self.r * self.r + self.i * self.i).sqrt();​
 +        let radical_other = (other.r * other.r + other.i * other.i).sqrt();​
 +        ​
 +        radical_self.partial_cmp(&​radical_other)
 +    }
 +}
 +
 +fn main () {
 +    let n1 = Complex::​new (2.0, 3.0);
 +    let n2 = Complex::​new (-2.0, 3.0);
 +    let n3 = Complex::​new (2.0, -3.0);
 +    let n4 = Complex::​new (3.0, 0.0);
 +    let n5 = Complex::​new (0.0, 3.0);
 +
 +    println! ("The number is {}", n1); // prints 2+3i
 +    println! ("The number is {}", n2); // prints -2+3i
 +    println! ("The number is {}", n3); // prints 2-3i
 +    println! ("The number is {}", n4); // prints 3
 +    println! ("The number is {}", n5); // prints 3i
 +
 +    println! ("The number is {}", n1-n1); // prints 0
 +    println! ("The number is {}", n1+n2); ​
 +    println! ("The number is {}", n1-n2);
 +    println! ("The number is {}", n1*n2);
 +    println! ("The numbers {} and {} are {}", n1, n2, if n1==n2 { "​equal"​} else {"not equal"​});​
 +    ​
 +    println! ("The number is {}", n1<n2);
 +    println! ("The number is {}", n1<​=n2); ​
 +    println! ("The number is {}", n1>n2);
 +    println! ("The number is {}", n1>=n2);
 } }
 </​code>​ </​code>​
  
  
saiot/laboratoare/02.1603310746.txt.gz ยท Last modified: 2020/10/21 23:05 by alexandru.radovici
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