finish ch10
This commit is contained in:
parent
54b05815ab
commit
e840d912d1
@ -3,6 +3,8 @@
|
|||||||
// behavior in an abstract way. Can use trait bounds to specify
|
// behavior in an abstract way. Can use trait bounds to specify
|
||||||
// that a generic type can be any type that has certain behavior
|
// that a generic type can be any type that has certain behavior
|
||||||
|
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
// Like Haskell's deriving
|
// Like Haskell's deriving
|
||||||
// Except orphans aren't allowed at all
|
// Except orphans aren't allowed at all
|
||||||
pub trait Summary {
|
pub trait Summary {
|
||||||
@ -89,6 +91,10 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// println!("The longest string is {result}");
|
// println!("The longest string is {result}");
|
||||||
|
|
||||||
|
// the static lifetime
|
||||||
|
// Can life for the duration of the program
|
||||||
|
let s: &'static str = "I have a static lifetime";
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn notify<T: Summary>(item1: &T, item2: &T) {}
|
// fn notify<T: Summary>(item1: &T, item2: &T) {}
|
||||||
@ -109,6 +115,7 @@ where
|
|||||||
// <'a> and 'a dispersed in the function parameters are
|
// <'a> and 'a dispersed in the function parameters are
|
||||||
// lifetime annotations. Lifetimes annotations are needed because
|
// lifetime annotations. Lifetimes annotations are needed because
|
||||||
// Rust can't tell whether the reference being returned refers to x or y
|
// Rust can't tell whether the reference being returned refers to x or y
|
||||||
|
// Feels like a similar vibe to Haskell `forall a`.
|
||||||
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
|
||||||
@ -121,6 +128,44 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
|||||||
// with references passed as a parameter have lifetime annotations
|
// with references passed as a parameter have lifetime annotations
|
||||||
// passed implicitly due to soooooo many functions having this
|
// passed implicitly due to soooooo many functions having this
|
||||||
// lifetime annotation pattern used.
|
// lifetime annotation pattern used.
|
||||||
|
//
|
||||||
|
// Lifetimes on function/method parameters called input lifetimes.
|
||||||
|
// Lifetimes on return values output lifetimes
|
||||||
|
//
|
||||||
|
// 3 Rules for lifetime functions
|
||||||
|
// 1: Compiler assigns different lifetime parameter to each lifetime
|
||||||
|
// in each input type
|
||||||
|
// fn foo(x: &i32) becomes fn foo<'a>(x: &'a i32)
|
||||||
|
// fn foo(x: &i32, y: &i32) becomes fn foo<'a, 'b>(x: &'a i32, y: &'b i32)
|
||||||
|
// fn foo(x: &ImportantExcerpt) becomes fn foo<'a, 'b>(x: &'a ImportantExcerpt<'b>)
|
||||||
|
// 2: If exactly one input lifetime paramter, lifetime applied to all output
|
||||||
|
// lifetime parameters
|
||||||
|
// 3: If multiple input lifetime parameters, but one of them is &self or &mut self,
|
||||||
|
// lifetime of self assigned to all output lifetime parameters
|
||||||
|
|
||||||
// Stopped here pre red river gorge:
|
// Lifetime annotations in method definitions
|
||||||
// https://rust-book.cs.brown.edu/ch10-03-lifetime-syntax.html#lifetime-elision
|
|
||||||
|
impl<'a> ImportantExcerpt<'a> {
|
||||||
|
fn level(&self) -> i32 {
|
||||||
|
3
|
||||||
|
}
|
||||||
|
|
||||||
|
// example of third lifetime elision rule
|
||||||
|
fn announce_and_return_part(&self, announcement: &str) -> &str {
|
||||||
|
println!("Attention Please: {announcement}");
|
||||||
|
self.part
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic Type Paramters, trait bounds, and lifetimes together
|
||||||
|
fn longest_with_an_announcement<'a, T>(x: &'a str, y: &'a str, ann: T) -> &'a str
|
||||||
|
where
|
||||||
|
T: Display,
|
||||||
|
{
|
||||||
|
println!("Announcement!: {ann}");
|
||||||
|
if x.len() > y.len() {
|
||||||
|
x
|
||||||
|
} else {
|
||||||
|
y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user