From bef6354109dbf7880cc094dbcac5c6af41042c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=A3=9E?= Date: Fri, 11 Feb 2022 14:42:25 +0800 Subject: [PATCH] add skeleton --- .gitignore | 1 + book.toml | 20 +++++++++++++++++ src/SUMMARY.md | 4 ++++ src/lifetime/intro.md | 1 + src/lifetime/static.md | 49 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 .gitignore create mode 100644 book.toml create mode 100644 src/SUMMARY.md create mode 100644 src/lifetime/intro.md create mode 100644 src/lifetime/static.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7585238 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +book diff --git a/book.toml b/book.toml new file mode 100644 index 0000000..8de752d --- /dev/null +++ b/book.toml @@ -0,0 +1,20 @@ +[book] +title = "Rust Exercise" +description = "A set of exercises and examples to pratice various aspects of Rust, also for the rust course (book) at https://course.rs" +authors = ["course.rs"] +language = "en" +multilingual = false +src = "src" + +[output.html.playpen] +editable = true +editor = "ace" + +[output.html.fold] +enable = true + +[output.html] +git-repository-url = "https://github.com/course-rs/rust-exercise" + +[rust] +edition = "2021" \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md new file mode 100644 index 0000000..67c21db --- /dev/null +++ b/src/SUMMARY.md @@ -0,0 +1,4 @@ +# Summary + +- [Lifetime](lifetime/intro.md) + - [&'static and T: 'static](lifetime/static.md) \ No newline at end of file diff --git a/src/lifetime/intro.md b/src/lifetime/intro.md new file mode 100644 index 0000000..2615315 --- /dev/null +++ b/src/lifetime/intro.md @@ -0,0 +1 @@ +# Lifetime diff --git a/src/lifetime/static.md b/src/lifetime/static.md new file mode 100644 index 0000000..782446e --- /dev/null +++ b/src/lifetime/static.md @@ -0,0 +1,49 @@ +# &'static and T: 'static + +```rust,editable +use std::fmt::Display; + +fn main() { + let mut string = "First".to_owned(); + + string.push_str(string.to_uppercase().as_str()); + print_a(&string); + print_b(&string); + print_c(&string); // Compilation error + print_d(&string); // Compilation error + print_e(&string); + print_f(&string); + print_g(&string); // Compilation error +} + +fn print_a(t: &T) { + println!("{}", t); +} + +fn print_b(t: &T) +where + T: Display + 'static, +{ + println!("{}", t); +} + +fn print_c(t: &'static dyn Display) { + println!("{}", t) +} + +fn print_d(t: &'static impl Display) { + println!("{}", t) +} + +fn print_e(t: &(dyn Display + 'static)) { + println!("{}", t) +} + +fn print_f(t: &(impl Display + 'static)) { + println!("{}", t) +} + +fn print_g(t: &'static String) { + println!("{}", t); +} +``` \ No newline at end of file