The following function fails as I would expect it to. This:
fn return_ref() -> &str {
let local_ref = &"world"[..];
local_ref
}
Fails with this:
Checking rust_learnings v0.1.0 (/home/red/code/rust_learnings)
error[E0106]: missing lifetime specifier
--> src/main.rs:18:20
|
18 | fn return_ref() -> &str {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
|
18 | fn return_ref() -> &'static str {
| ~~~~~~~~
For more information about this error, try `rustc --explain E0106`.
error: could not compile `rust_learnings` due to previous error
However if I add a (specifically) reference to a string in, like so:
fn return_ref(passed_ref: &str) -> &str {
let local_ref = &"world"[..];
local_ref
}
My only problem is that the passed in reference warns of not being used, otherwise it runs with no error. How is this possible?