traits done

This commit is contained in:
David E. Perez Negron R. 2024-05-11 11:44:39 -06:00
parent 5a374a4588
commit 0ac5f3a1e3
5 changed files with 14 additions and 8 deletions

View File

@ -7,7 +7,6 @@
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a // Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
trait AppendBar { trait AppendBar {
fn append_bar(self) -> Self; fn append_bar(self) -> Self;
@ -15,6 +14,9 @@ trait AppendBar {
impl AppendBar for String { impl AppendBar for String {
// TODO: Implement `AppendBar` for type `String`. // TODO: Implement `AppendBar` for type `String`.
fn append_bar(self) -> Self {
format!("{self}Bar")
}
} }
fn main() { fn main() {

View File

@ -8,13 +8,18 @@
// //
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
trait AppendBar { trait AppendBar {
fn append_bar(self) -> Self; fn append_bar(self) -> Self;
} }
// TODO: Implement trait `AppendBar` for a vector of strings. // 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)] #[cfg(test)]
mod tests { mod tests {

View File

@ -8,10 +8,11 @@
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a // Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
pub trait Licensed { pub trait Licensed {
fn licensing_info(&self) -> String; fn licensing_info(&self) -> String{
format!("Some information")
}
} }
struct SomeSoftware { struct SomeSoftware {

View File

@ -7,7 +7,6 @@
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a // Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
pub trait Licensed { pub trait Licensed {
fn licensing_info(&self) -> String { fn licensing_info(&self) -> String {
@ -23,7 +22,7 @@ impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {} impl Licensed for OtherSoftware {}
// YOU MAY ONLY CHANGE THE NEXT LINE // 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() software.licensing_info() == software_two.licensing_info()
} }

View File

@ -7,7 +7,6 @@
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a // Execute `rustlings hint traits5` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
pub trait SomeTrait { pub trait SomeTrait {
fn some_function(&self) -> bool { fn some_function(&self) -> bool {
@ -30,7 +29,7 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {} impl OtherTrait for OtherStruct {}
// YOU MAY ONLY CHANGE THE NEXT LINE // 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() item.some_function() && item.other_function()
} }