mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
done till exercises/quiz3
This commit is contained in:
parent
782076dbcc
commit
6623e38988
@ -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<i32>.
|
||||
|
||||
// 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<i32> = 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");
|
||||
}
|
||||
|
||||
@ -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<T> {
|
||||
value: T,
|
||||
}
|
||||
|
||||
impl Wrapper {
|
||||
pub fn new(value: u32) -> Self {
|
||||
impl<T> Wrapper<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Wrapper { value }
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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<String> {
|
||||
// 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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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<T, U>(software: T, software_two: U) -> bool
|
||||
where
|
||||
T: Licensed,
|
||||
U: Licensed,
|
||||
{
|
||||
software.licensing_info() == software_two.licensing_info()
|
||||
}
|
||||
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user