Skip to content

Commit

Permalink
15 traits ~ interface, trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
chiffonng committed Jun 22, 2024
1 parent b207b3a commit e4d2c30
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
3 changes: 2 additions & 1 deletion exercises/15_traits/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Traits

A trait is a collection of methods.
A trait is a collection of methods. It is roughly similar to an interface in other languages. Traits are used to define shared behavior between data types.

Data types can implement traits. To do so, the methods making up the trait are defined for the data type. For example, the `String` data type implements the `From<&str>` trait. This allows a user to write `String::from("hello")`.

Expand All @@ -17,3 +17,4 @@ Because traits indicate shared behavior between data types, they are useful when
## Further information

- [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html)
- [Trait as parameters](https://doc.rust-lang.org/stable/book/ch10-02-traits.html#traits-as-parameters)
6 changes: 4 additions & 2 deletions exercises/15_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
// https://doc.rust-lang.org/book/ch10-02-traits.html

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

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

fn main() {
Expand Down
10 changes: 8 additions & 2 deletions exercises/15_traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
//
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE
// https://dhghomon.github.io/easy_rust/Chapter_21.html

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

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

#[cfg(test)]
mod tests {
Expand Down
14 changes: 11 additions & 3 deletions exercises/15_traits/traits3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// 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;
Expand All @@ -22,8 +22,16 @@ struct OtherSoftware {
version_number: String,
}

impl Licensed for SomeSoftware {} // Don't edit this line
impl Licensed for OtherSoftware {} // Don't edit this line
impl Licensed for SomeSoftware {
fn licensing_info(&self) -> String {
String::from("Some information")
}
}
impl Licensed for OtherSoftware {
fn licensing_info(&self) -> String {
String::from("Some information")
}
}

#[cfg(test)]
mod tests {
Expand Down
9 changes: 5 additions & 4 deletions exercises/15_traits/traits4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

// Default implementation for a trait
// https://doc.rust-lang.org/stable/book/ch10-02-traits.html#default-implementations
pub trait Licensed {
fn licensing_info(&self) -> String {
"some information".to_string()
Expand All @@ -22,8 +22,9 @@ struct OtherSoftware {}
impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}

// YOU MAY ONLY CHANGE THE NEXT LINE
fn compare_license_types(software: ??, software_two: ??) -> bool {
// Two parameters have generic types T and U, both of which must implement the Licensed trait. This allows the function to accept any two types that implement Licensed, including SomeSoftware and OtherSoftware.
// https://doc.rust-lang.org/stable/book/ch10-02-traits.html#trait-bound-syntax
fn compare_license_types<T: Licensed, U: Licensed>(software: T, software_two: U) -> bool {
software.licensing_info() == software_two.licensing_info()
}

Expand Down
7 changes: 4 additions & 3 deletions exercises/15_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
// https://doc.rust-lang.org/stable/book/ch10-02-traits.html#specifying-multiple-trait-bounds-with-the--syntax

pub trait SomeTrait {
fn some_function(&self) -> bool {
Expand All @@ -29,8 +29,9 @@ impl OtherTrait for SomeStruct {}
impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}

// YOU MAY ONLY CHANGE THE NEXT LINE
fn some_func(item: ??) -> bool {
// Item is a generic type that must implement SomeTrait or OtherTrait (or both).
// Use + to specify multiple trait bounds.
fn some_func<T: SomeTrait + OtherTrait>(item: T) -> bool {
item.some_function() && item.other_function()
}

Expand Down

0 comments on commit e4d2c30

Please sign in to comment.