finish till quiz3

This commit is contained in:
Sangeeth Sudheer 2024-02-02 05:54:03 +05:30
parent c415145547
commit 4f4b6f5194
Signed by: x
GPG Key ID: F6D06ECE734C57D1
11 changed files with 30 additions and 38 deletions

View File

@ -6,9 +6,7 @@
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a // Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn main() { fn main() {
let mut shopping_list: Vec<?> = Vec::new(); let mut shopping_list: Vec<&str> = Vec::new();
shopping_list.push("milk"); shopping_list.push("milk");
} }

View File

@ -6,14 +6,12 @@
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a // Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE struct Wrapper<T> {
value: T,
struct Wrapper {
value: u32,
} }
impl Wrapper { impl<T> Wrapper<T> {
pub fn new(value: u32) -> Self { pub fn new(value: T) -> Self {
Wrapper { value } Wrapper { value }
} }
} }

View File

@ -8,9 +8,7 @@
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a // Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() { if x.len() > y.len() {
x x
} else { } else {

View File

@ -6,8 +6,6 @@
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a // Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { if x.len() > y.len() {
x x
@ -22,6 +20,6 @@ fn main() {
{ {
let string2 = String::from("xyz"); let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str()); result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is '{}'", result); println!("The longest string is '{}'", result);
}
} }

View File

@ -5,11 +5,9 @@
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a // Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE struct Book<'a> {
author: &'a str,
struct Book { title: &'a str,
author: &str,
title: &str,
} }
fn main() { fn main() {

View File

@ -16,15 +16,15 @@
// //
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE use std::fmt::Display;
pub struct ReportCard { pub struct ReportCard<T> {
pub grade: f32, pub grade: T,
pub student_name: String, pub student_name: String,
pub student_age: u8, pub student_age: u8,
} }
impl ReportCard { impl<T: Display> ReportCard<T> {
pub fn print(&self) -> String { pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}", format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade) &self.student_name, &self.student_age, &self.grade)
@ -52,7 +52,7 @@ mod tests {
fn generate_alphabetic_report_card() { fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise. // TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: "A+",
student_name: "Gary Plotter".to_string(), student_name: "Gary Plotter".to_string(),
student_age: 11, student_age: 11,
}; };

View File

@ -7,14 +7,15 @@
// 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;
} }
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 {
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(self) -> Self {
let mut cloned = self.clone();
cloned.push(String::from("Bar"));
cloned
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -8,10 +8,10 @@
// 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 {
"Some information".into()
}
} }
struct SomeSoftware { struct SomeSoftware {

View File

@ -7,8 +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 {
"some information".to_string() "some information".to_string()
@ -23,7 +21,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(software: impl Licensed, software_two: impl Licensed) -> bool {
software.licensing_info() == software_two.licensing_info() software.licensing_info() == software_two.licensing_info()
} }

View File

@ -7,8 +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 {
true true
@ -30,7 +28,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(item: impl SomeTrait + OtherTrait) -> bool {
item.some_function() && item.other_function() item.some_function() && item.other_function()
} }