Skip to content

Commit

Permalink
another batch.
Browse files Browse the repository at this point in the history
  • Loading branch information
codec-abc committed Jan 11, 2021
1 parent 23bca7c commit 7b28611
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
4 changes: 1 addition & 3 deletions exercises/generics/generics1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// This shopping list program isn't compiling!
// Use your knowledge of generics to fix it.

// I AM NOT DONE

fn main() {
let mut shopping_list: Vec<?> = Vec::new();
let mut shopping_list: Vec<&str> = Vec::new();
shopping_list.push("milk");
}
10 changes: 4 additions & 6 deletions exercises/generics/generics2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// This powerful wrapper provides the ability to store a positive integer value.
// Rewrite it using generics so that it supports wrapping ANY type.

// 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 }
}
}
Expand Down
10 changes: 5 additions & 5 deletions exercises/generics/generics3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

// Execute 'rustlings hint generics3' for hints!

// I AM NOT DONE
use std::fmt::Display;

pub struct ReportCard {
pub grade: f32,
pub struct ReportCard<T> where T: Display {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}

impl ReportCard {
impl<T: Display> ReportCard<T> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade)
Expand Down Expand Up @@ -46,7 +46,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+",
student_name: "Gary Plotter".to_string(),
student_age: 11,
};
Expand Down
10 changes: 4 additions & 6 deletions exercises/option/option1.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
// option1.rs
// Make me compile! Execute `rustlings hint option1` for hints

// I AM NOT DONE

// you can modify anything EXCEPT for this function's sig
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] = [None; 5];
for iter in 0..5 {
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);
}
}
6 changes: 2 additions & 4 deletions exercises/option/option2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// option2.rs
// Make me compile! Execute `rustlings hint option2` for hints

// I AM NOT DONE

fn main() {
let optional_value = Some(String::from("rustlings"));
// TODO: Make this an if let statement whose value is "Some" type
value = optional_value {
if let Some(value) = optional_value {
println!("the value of optional value is: {}", value);
} else {
println!("The optional value doesn't contain anything!");
Expand All @@ -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
value = optional_values_vec.pop() {
while let Some(Some(value)) = optional_values_vec.pop() {
println!("current value: {}", value);
}
}

0 comments on commit 7b28611

Please sign in to comment.