Como a vida útil funciona em strings / literais de strings constantes?
Eu li otutorial no site oficial e eu tenho algumas perguntas sobre o tempo de vida de constantes strings / strings literais.
Eu recebo um erro quando escrevo o seguinte código:
fn get_str() -> &str {
"Hello World"
}
erro:
error[E0106]: missing lifetime specifier
--> src/main.rs:1:17
|
1 | fn get_str() -> &str {
| ^ expected 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 giving it a 'static lifetime
No entanto, tudo bem quando adiciono um parâmetro:
fn get_str(s: &str) -> &str {
"Hello World"
}
Por que isso funciona? Como"Hello World"
emprestado do parâmetros
, mesmo que não tenha nada a ver coms
?