Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyazi233 committed Apr 8, 2024
1 parent be76387 commit cbe8c7c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 30 deletions.
9 changes: 4 additions & 5 deletions exercises/iterators/iterators1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];

let mut my_iterable_fav_fruits = ???; // TODO: Step 1
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1

assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
}
4 changes: 2 additions & 2 deletions exercises/lifetimes/lifetimes1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn longest(x: &str, y: &str) -> &str {

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
Expand Down
7 changes: 3 additions & 4 deletions exercises/lifetimes/lifetimes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
fn longest(x: &str, y: &str) -> String {
if x.len() > y.len() {
x
x.to_string()
} else {
y
y.to_string()
}
}

Expand Down
8 changes: 4 additions & 4 deletions exercises/lifetimes/lifetimes3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct Book {
author: &str,
title: &str,

struct Book<'a> {
author: &'a str,
title: &'a str,
}

fn main() {
Expand Down
18 changes: 13 additions & 5 deletions exercises/quiz3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,29 @@
//
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

pub struct ReportCard {
pub grade: f32,

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

impl ReportCard {
impl ReportCard<&str> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade)
}
}

impl ReportCard<f32> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {:.1}",
&self.student_name, &self.student_age, &self.grade)
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -52,7 +60,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
4 changes: 2 additions & 2 deletions exercises/tests/tests1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


#[cfg(test)]
mod tests {
#[test]
fn you_can_assert() {
assert!();
assert!(true);
}
}
4 changes: 2 additions & 2 deletions exercises/tests/tests2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


#[cfg(test)]
mod tests {
#[test]
fn you_can_assert_eq() {
assert_eq!();
assert_eq!(1,1);
}
}
6 changes: 3 additions & 3 deletions exercises/tests/tests3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


pub fn is_even(num: i32) -> bool {
num % 2 == 0
Expand All @@ -19,11 +19,11 @@ mod tests {

#[test]
fn is_true_when_even() {
assert!();
assert!(is_even(2));
}

#[test]
fn is_false_when_odd() {
assert!();
assert!(!is_even(3));
}
}
8 changes: 5 additions & 3 deletions exercises/tests/tests4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


struct Rectangle {
width: i32,
Expand All @@ -30,17 +30,19 @@ mod tests {
fn correct_width_and_height() {
// This test should check if the rectangle is the size that we pass into its constructor
let rect = Rectangle::new(10, 20);
assert_eq!(???, 10); // check width
assert_eq!(???, 20); // check height
assert_eq!(rect.width, 10); // check width
assert_eq!(rect.height, 20); // check height
}

#[test]
#[should_panic]
fn negative_width() {
// This test should check if program panics when we try to create rectangle with negative width
let _rect = Rectangle::new(-10, 10);
}

#[test]
#[should_panic]
fn negative_height() {
// This test should check if program panics when we try to create rectangle with negative height
let _rect = Rectangle::new(10, -10);
Expand Down

0 comments on commit cbe8c7c

Please sign in to comment.