Skip to content

Commit

Permalink
push 57-63 generics trait
Browse files Browse the repository at this point in the history
  • Loading branch information
imizao committed Apr 11, 2024
1 parent c521da0 commit ffb9e7e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
3 changes: 1 addition & 2 deletions exercises/generics/generics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
// hint.

// 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: 5 additions & 5 deletions exercises/generics/generics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
// hint.

// 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
4 changes: 3 additions & 1 deletion exercises/traits/traits1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

trait AppendBar {
fn append_bar(self) -> Self;
}

impl AppendBar for String {
// TODO: Implement `AppendBar` for type `String`.
fn append_bar(self) -> String {
self + "Bar"
}
}

fn main() {
Expand Down
7 changes: 6 additions & 1 deletion exercises/traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
//
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

trait AppendBar {
fn append_bar(self) -> Self;
}

// TODO: Implement trait `AppendBar` for a vector of strings.
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push(String::from("Bar"));
self
}
}

#[cfg(test)]
mod tests {
Expand Down
5 changes: 3 additions & 2 deletions exercises/traits/traits3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

pub trait Licensed {
fn licensing_info(&self) -> String;
fn licensing_info(&self) -> String{
String::from("Some information")
}
}

struct SomeSoftware {
Expand Down
3 changes: 1 addition & 2 deletions exercises/traits/traits4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

pub trait Licensed {
fn licensing_info(&self) -> String {
Expand All @@ -23,7 +22,7 @@ impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}

// YOU MAY ONLY CHANGE THE NEXT LINE
fn compare_license_types(software: ??, software_two: ??) -> bool {
fn compare_license_types<T: Licensed, U: Licensed>(software: T, software_two: U) -> bool {
software.licensing_info() == software_two.licensing_info()
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/traits/traits5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


pub trait SomeTrait {
fn some_function(&self) -> bool {
Expand All @@ -30,7 +30,7 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}

// YOU MAY ONLY CHANGE THE NEXT LINE
fn some_func(item: ??) -> bool {
fn some_func<T: SomeTrait + OtherTrait>(item: T) -> bool {
item.some_function() && item.other_function()
}

Expand Down

0 comments on commit ffb9e7e

Please sign in to comment.