traits done

This commit is contained in:
Chris Girvin 2022-05-22 05:33:23 -04:00
parent 6ffd13fbe2
commit 12186f4a30
7 changed files with 35 additions and 34 deletions

View File

@ -3,14 +3,12 @@
// Execute `rustlings hint generics2` for hints!
// I AM NOT DONE
struct Wrapper {
value: u32,
struct Wrapper<T> {
value: T,
}
impl Wrapper {
pub fn new(value: u32) -> Self {
impl<T> Wrapper<T> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}

View File

@ -10,18 +10,20 @@
// Execute 'rustlings hint generics3' for hints!
// I AM NOT DONE
use std::fmt::Display;
pub struct ReportCard {
pub grade: f32,
pub struct ReportCard<T: Display> {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}
impl ReportCard {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade)
impl<T: Display> ReportCard<T> {
fn print(&self) -> String {
format!(
"{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade
)
}
}
@ -46,7 +48,7 @@ mod tests {
fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard {
grade: 2.1,
grade: "A+".to_string(),
student_name: "Gary Plotter".to_string(),
student_age: 11,
};

View File

@ -1,23 +1,19 @@
// option1.rs
// Make me compile! Execute `rustlings hint option1` for hints
// I AM NOT DONE
// you can modify anything EXCEPT for this function's signature
fn print_number(maybe_number: Option<u16>) {
println!("printing: {}", maybe_number.unwrap());
}
fn main() {
print_number(13);
print_number(99);
print_number(Some(13));
print_number(Some(99));
let mut numbers: [Option<u16>; 5];
let mut numbers: [Option<u16>; 5] = [Some(0); 5];
for iter in 0..5 {
let number_to_add: u16 = {
((iter * 1235) + 2) / (4 * 16)
};
let number_to_add: u16 = { ((iter * 1235) + 2) / (4 * 16) };
numbers[iter as usize] = number_to_add;
numbers[iter as usize] = Some(number_to_add);
}
}

View File

@ -1,13 +1,11 @@
// option2.rs
// Make me compile! Execute `rustlings hint option2` for hints
// I AM NOT DONE
fn main() {
let optional_word = Some(String::from("rustlings"));
// TODO: Make this an if let statement whose value is "Some" type
word = optional_word {
println!("The word is: {}", word);
if let word = optional_word {
println!("The word is: {:?}", word);
} else {
println!("The optional word doesn't contain anything");
}
@ -19,7 +17,7 @@ fn main() {
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
// You can stack `Option<T>`'s into while let and if let
integer = optional_integers_vec.pop() {
println!("current value: {}", integer);
while let Some(integer) = optional_integers_vec.pop() {
println!("current value: {:?}", integer);
}
}

View File

@ -1,8 +1,6 @@
// option3.rs
// Make me compile! Execute `rustlings hint option3` for hints
// I AM NOT DONE
struct Point {
x: i32,
y: i32,
@ -12,7 +10,7 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => println!("no match"),
}
y; // Fix without deleting this line.

View File

@ -8,14 +8,17 @@
// which appends "Bar" to any object
// implementing this trait.
// I AM NOT DONE
trait AppendBar {
fn append_bar(self) -> Self;
}
impl AppendBar for String {
//Add your code here
fn append_bar(self) -> String {
let mut out = String::from(&self);
out.push_str("Bar");
out.to_string()
}
}
fn main() {

View File

@ -17,6 +17,12 @@ trait AppendBar {
}
//TODO: Add your code here
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Vec<String> {
self.append(&mut vec!["Bar".to_string()]);
self
}
}
#[cfg(test)]
mod tests {