generics and lifetimes

This commit is contained in:
2026-01-22 01:54:12 +00:00
parent b8c7b5e68b
commit c258b53e52
7 changed files with 163 additions and 0 deletions

6
lifetimes/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "lifetimes"
version = "0.1.0"
edition = "2024"
[dependencies]

26
lifetimes/src/main.rs Normal file
View File

@@ -0,0 +1,26 @@
// fn main() {
// let string1 = String::from("abcd");
// let string2 = String::from("xyz");
// let result = longest(string1.as_str(), string2.as_str());
// println!("The longer string is: {result}");
// }
//
// fn longest<'somelifetime>(a: &'somelifetime str, b: &'somelifetime str) -> &'somelifetime str {
// if a.len() > b.len() { a } else { b }
// }
//
struct ImportantExcerpt<'a> {
part: &'a str,
}
//
// fn return_excerpt(story: ImportantExcerpt) -> &'a str {
// let first_sentence = novel.split('.')
// }
fn main() {
let novel = String::from("Call me Ishmael. Some years ago...");
let first_sentence = novel.split('.').next().unwrap();
let i = ImportantExcerpt {
part: first_sentence,
};
}