diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index 4c34ae47..642de2d3 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -3,9 +3,22 @@ // Execute `rustlings hint generics1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE +// ChatGPT +// Vec<_> is a shorthand syntax in Rust for declaring a vector with the element type left unspecified. + +// In Rust, a vector is a dynamically resizable array that allows you to store and manipulate a collection of elements. When declaring a vector, you typically specify the element type that the vector will contain. For example, to create a vector of integers, you would declare it as Vec. + +// However, if you want to declare a vector without specifying the element type, you can use the underscore (_) as a placeholder. This is useful when you want the compiler to infer the element type based on the values you insert into the vector. + +// For example, the following code creates a vector of integers and inserts the values 1, 2, and 3 into it: + +// let v: Vec<_> = vec![1, 2, 3]; +// Here, the underscore allows the compiler to infer that the vector contains integers. If you were to remove the underscore, you would have to explicitly specify the element type as i32: + +// let v: Vec = vec![1, 2, 3]; fn main() { - let mut shopping_list: Vec = Vec::new(); + let mut shopping_list: Vec<_> = Vec::new(); + // let mut shopping_list: Vec<&str> = Vec::new(); shopping_list.push("milk"); } diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs index aedbd55c..ef084440 100644 --- a/exercises/generics/generics2.rs +++ b/exercises/generics/generics2.rs @@ -3,14 +3,12 @@ // Execute `rustlings hint generics2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -struct Wrapper { - value: u32, +struct Wrapper { + value: T, } -impl Wrapper { - pub fn new(value: u32) -> Self { +impl Wrapper { + pub fn new(value: T) -> Self { Wrapper { value } } } diff --git a/exercises/traits/README.md b/exercises/traits/README.md index de67acd0..ac87c64e 100644 --- a/exercises/traits/README.md +++ b/exercises/traits/README.md @@ -7,13 +7,13 @@ Data types can implement traits. To do so, the methods making up the trait are d In this way, traits are somewhat similar to Java interfaces and C++ abstract classes. Some additional common Rust traits include: + - `Clone` (the `clone` method) - `Display` (which allows formatted display via `{}`) - `Debug` (which allows formatted display via `{:?}`) Because traits indicate shared behavior between data types, they are useful when writing generics. - ## Further information - [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html) diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index f5320a5a..61c4d09a 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -9,14 +9,15 @@ // implementing this trait. // 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) -> Self { + format!("{}{}", self, "Bar") + } } fn main() { diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 288b4983..35166c4c 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -11,13 +11,19 @@ // you can do this! // 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 { + // In other words, when a value is owned, its mutability is not part of the type, only the binding. When calling a function or implementing a trait, only the types have to match, not the patterns of the parameters + // When a value is moved, it doesn't matter for the method declaration whether or not it is mutated. An owned value can always be made mutable anyway: let mut x = x; + fn append_bar(mut self) -> Self { + self.push("Bar".into()); + self + } +} #[cfg(test)] mod tests { diff --git a/exercises/traits/traits3.rs b/exercises/traits/traits3.rs index 6d2fd6c3..86e425fa 100644 --- a/exercises/traits/traits3.rs +++ b/exercises/traits/traits3.rs @@ -7,10 +7,10 @@ // Consider what you can add to the Licensed trait. // 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 { diff --git a/exercises/traits/traits4.rs b/exercises/traits/traits4.rs index 6b541665..d0151aad 100644 --- a/exercises/traits/traits4.rs +++ b/exercises/traits/traits4.rs @@ -4,8 +4,6 @@ // Don't change any line other than the marked one. // 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 { "some information".to_string() @@ -20,7 +18,11 @@ 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: T, software_two: U) -> bool +where + T: Licensed, + U: Licensed, +{ software.licensing_info() == software_two.licensing_info() } diff --git a/exercises/traits/traits5.rs b/exercises/traits/traits5.rs index 0fbca28a..b6378a10 100644 --- a/exercises/traits/traits5.rs +++ b/exercises/traits/traits5.rs @@ -4,8 +4,6 @@ // Don't change any line other than the marked one. // 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 { true @@ -27,7 +25,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() }