Skip to content

Commit

Permalink
push 64-67 lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
imizao committed Apr 11, 2024
1 parent ffb9e7e commit f0ae56a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
3 changes: 1 addition & 2 deletions exercises/lifetimes/lifetimes1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn longest(x: &str, y: &str) -> &str {
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
Expand Down
3 changes: 1 addition & 2 deletions exercises/lifetimes/lifetimes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
Expand All @@ -18,9 +17,9 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {

fn main() {
let string1 = String::from("long string is long");
let string2 = String::from("xyz");
let result;
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is '{}'", result);
Expand Down
7 changes: 3 additions & 4 deletions exercises/lifetimes/lifetimes3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct Book {
author: &str,
title: &str,
struct Book<'a> {
author: &'a str,
title: &'a str,
}

fn main() {
Expand Down
9 changes: 4 additions & 5 deletions exercises/quiz3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
//
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

pub struct ReportCard {
pub grade: f32,
pub struct ReportCard<T> {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}

impl ReportCard {
impl<T: std::fmt::Display> ReportCard<T> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade)
Expand Down Expand Up @@ -52,7 +51,7 @@ mod tests {
fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard {
grade: 2.1,
grade: "A+".to_string(),
student_name: "Gary Plotter".to_string(),
student_age: 11,
};
Expand Down

0 comments on commit f0ae56a

Please sign in to comment.