#![allow(dead_code)] fn main() { struct Color(i32, i32, i32); let c = Color(0, 255, 255); println!("({}, {}, {})", c.0, c.1, c.2); }
#![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); }
Solution: Ioana CIUPITU
struct Professor { firstname: String, lastname: String, age: usize, } struct Student { firstname: String, lastname: String, age: usize, } trait Person { fn get_name (&self) -> String; fn get_job (&self) -> String; } impl Professor { fn new(firstname: String, lastname: String, age: usize) -> Professor { Professor { firstname: firstname, lastname: lastname, age:age } } } impl Person for Professor { fn get_name (&self) -> String { format!("{} {}", self.firstname, self.lastname) } fn get_job (&self) -> String { "student".to_string() } } impl Student { fn new(firstname: String, lastname: String, age: usize) -> Student { Student { firstname: firstname, lastname: lastname, age:age } } } impl Person for Student { fn get_name (&self) -> String { format!("{}", self.firstname) } fn get_job (&self) -> String { return "profesor".to_string(); } } fn main () { // make a professor let p = Professor::new (String::from("John"), String::from("Doe"), 20); // make a student let s = Student::new ("Johnny".to_string(), "Doe".to_string(), 50); // println! ("{:?}", p); // println! ("{:?}", s); println! ("{} is a {}", p.get_name (), p.get_job()); println! ("{} is a {}", s.get_name (), s.get_job()); }
Solution: Alexandru HOGEA
#![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); }
Solution: Alexandru HOGEA
#![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"}); }
Solution: Alexandru HOGEA
#![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); }