Skip to content

Commit

Permalink
traits exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaopmorais committed Feb 20, 2024
1 parent 3eadd1e commit ad1170b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
5 changes: 3 additions & 2 deletions exercises/15_traits/traits1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
// 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 {
format!("{}Bar", self)
}
}

fn main() {
Expand Down
10 changes: 9 additions & 1 deletion exercises/15_traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
//
// 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("Bar".to_string());
self
}

}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 3 additions & 2 deletions exercises/15_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/15_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(software: impl Licensed, software_two: impl Licensed) -> bool {
software.licensing_info() == software_two.licensing_info()
}

Expand Down
3 changes: 1 addition & 2 deletions exercises/15_traits/traits5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// 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 +29,7 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}

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

Expand Down

0 comments on commit ad1170b

Please sign in to comment.