summaryrefslogtreecommitdiff
path: root/exercises/lifetimes/lifetimes1.rs
diff options
context:
space:
mode:
authorjayber <james.a.bromley@gmail.com>2022-07-15 14:01:32 +0200
committermokou <mokou@fastmail.com>2022-07-15 14:03:38 +0200
commit1ef8dacaf69e3ad1418355eeea128c00f09bdcb3 (patch)
treeace3826ecdf6f3a6c110bd11477f93d9f4847c70 /exercises/lifetimes/lifetimes1.rs
parent1cc5df0e144ef3551062c162e2e1618c27d97ae0 (diff)
feat: add lifetimes exercises
Diffstat (limited to 'exercises/lifetimes/lifetimes1.rs')
-rw-r--r--exercises/lifetimes/lifetimes1.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/exercises/lifetimes/lifetimes1.rs b/exercises/lifetimes/lifetimes1.rs
new file mode 100644
index 0000000..58e995c
--- /dev/null
+++ b/exercises/lifetimes/lifetimes1.rs
@@ -0,0 +1,26 @@
+// lifetimes1.rs
+//
+// The Rust compiler needs to know how to check whether supplied references are
+// valid, so that it can let the programmer know if a reference is at risk
+// of going out of scope before it is used. Remember, references are borrows
+// and do not own their own data. What if their owner goes out of scope?
+//
+// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a hint.
+
+// I AM NOT DONE
+
+fn longest(x: &str, y: &str) -> &str {
+ if x.len() > y.len() {
+ x
+ } else {
+ y
+ }
+}
+
+fn main() {
+ let string1 = String::from("abcd");
+ let string2 = "xyz";
+
+ let result = longest(string1.as_str(), string2);
+ println!("The longest string is {}", result);
+}