commit a754093bd02ec5e5ab8731071bead12379cfdc5a Author: Fabian Schmidt Date: Tue Jan 23 10:48:58 2024 +0100 Example of an error when lifetime not specified diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2611acc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "lifetimes" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..bc3f262 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "lifetimes" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2c19b59 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,15 @@ +fn main() { + let s1 = "Hello worlc"; + let s2 = "Hello"; + + let result: &str = longest(s1, s2); + println!("The longest string is {result}"); +} + +fn longest(a: &str, b: &str) -> &str { + if a.len() > b.len() { + a + } else { + b + } +}