Skip to content

Commit 4ca4f51

Browse files
Lifetime annotations in struct definitions.
1 parent 003ec56 commit 4ca4f51

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

rustlang_book/lifetimes/src/main.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
pub struct ImportantExcerpt<'a> {
2+
part: &'a str,
3+
}
4+
15
fn main() {
26
let string1 = String::from("abcd");
37
let string2 = "xyz";
48

59
let result = longest(string1.as_str(), string2);
610
println!("The longest string is {}", result);
11+
12+
let novel = String::from("Once upon a time there existed a crab. He was called a rustacean...");
13+
let first_sentence = novel.split('.').next().expect("Could not find a .");
14+
let i = ImportantExcerpt {
15+
part : first_sentence,
16+
};
17+
println!("The first sentence is: {}" , i.part);
718
}
819

920
fn longest<'a>(string1: &'a str, string2: &'a str) -> &'a str {

0 commit comments

Comments
 (0)