Skip to content

Commit

Permalink
finish traits
Browse files Browse the repository at this point in the history
  • Loading branch information
Withsan committed Oct 8, 2023
1 parent b0fa445 commit 6da07ec
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion exercises/traits/traits1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ trait AppendBar {
}

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

fn main() {
Expand Down
8 changes: 7 additions & 1 deletion exercises/traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
trait AppendBar {
fn append_bar(self) -> Self;
}

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

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion exercises/traits/traits3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
// 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
2 changes: 1 addition & 1 deletion exercises/traits/traits4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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(software: impl Licensed, software_two: impl Licensed) -> bool {
software.licensing_info() == software_two.licensing_info()
}

Expand Down
5 changes: 4 additions & 1 deletion exercises/traits/traits5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}

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

Expand Down

0 comments on commit 6da07ec

Please sign in to comment.