Skip to content

Commit

Permalink
traits and lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrx123 committed Apr 15, 2024
1 parent 113baf4 commit 6dfab6b
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 42 deletions.
2 changes: 0 additions & 2 deletions exercises/14_generics/generics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct Wrapper<T> {
value: T,
}
Expand Down
11 changes: 4 additions & 7 deletions exercises/15_traits/traits1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// 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").to_string()
}
}

fn main() {
Expand All @@ -34,9 +34,6 @@ mod tests {

#[test]
fn is_bar_bar() {
assert_eq!(
String::from("").append_bar().append_bar(),
String::from("BarBar")
);
assert_eq!(String::from("").append_bar().append_bar(), String::from("BarBar"));
}
}
9 changes: 6 additions & 3 deletions exercises/15_traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
//
// 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> {
fn append_bar(mut self) -> Self {
self.push(String::from("Bar"));
self
}
}

#[cfg(test)]
mod tests {
Expand Down
6 changes: 3 additions & 3 deletions exercises/15_traits/traits3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// 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 {
Expand Down
4 changes: 1 addition & 3 deletions exercises/15_traits/traits4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// 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()
Expand All @@ -23,7 +21,7 @@ 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: impl Licensed, software_two: impl Licensed) -> bool {
software.licensing_info() == software_two.licensing_info()
}

Expand Down
4 changes: 1 addition & 3 deletions exercises/15_traits/traits5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// 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
Expand All @@ -30,7 +28,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()
}

Expand Down
10 changes: 2 additions & 8 deletions exercises/16_lifetimes/lifetimes1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +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 {
if x.len() > y.len() {
x
} else {
y
}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}

fn main() {
Expand Down
10 changes: 2 additions & 8 deletions exercises/16_lifetimes/lifetimes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@
// 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() {
x
} else {
y
}
fn longest<'a>(x: &'a str, y: &str) -> &'a str {
x
}

fn main() {
Expand Down
8 changes: 3 additions & 5 deletions exercises/16_lifetimes/lifetimes3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
// 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

0 comments on commit 6dfab6b

Please sign in to comment.