Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

traits exercises #15

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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