I am trying to make a simple function to return the command line arguments. Code looks like this:
use std::env;
fn main() {
let (query, filename) = parse();
}
fn parse() -> (&str, &str) {
let args: Vec<String> = env::args().collect();
let query = args[1];
let filename = args[2];
return (query, filename)
}
However it will not compile, the errors look like this:
error[E0106]: missing lifetime specifier
--> src/main.rs:15:22
|
15 | fn parse() -> (&str, &str) {
| ^ help: consider giving it a 'static lifetime: `&'static`
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
It suggests that I need to add &'static
to the function declaration, like this:
fn parse() -> (&'static str, &'static str) {
But that does not work either;
error[E0308]: mismatched types
--> src/main.rs:20:13
|
20 | return (query, filename)
| ^^^^^
| |
| expected reference, found struct `std::string::String`
| help: consider borrowing here: `&query`
|
= note: expected type `&'static str`
found type `std::string::String`
error[E0308]: mismatched types
--> src/main.rs:20:20
|
20 | return (query, filename)
| ^^^^^^^^
| |
| expected reference, found struct `std::string::String`
| help: consider borrowing here: `&filename`
|
= note: expected type `&'static str`
found type `std::string::String`
It says I need to add borrowing, like this:
return (&query, &filename)
But that does not work either;
warning: unused variable: `query`
--> src/main.rs:5:10
|
5 | let (query, filename) = parse();
| ^^^^^ help: consider prefixing with an underscore: `_query`
|
= note: #[warn(unused_variables)] on by default
warning: unused variable: `filename`
--> src/main.rs:5:17
|
5 | let (query, filename) = parse();
| ^^^^^^^^ help: consider prefixing with an underscore: `_filename`
error[E0507]: cannot move out of borrowed content
--> src/main.rs:17:17
|
17 | let query = args[1];
| ^^^^^^^
| |
| cannot move out of borrowed content
| help: consider borrowing here: `&args[1]`
error[E0507]: cannot move out of borrowed content
--> src/main.rs:18:20
|
18 | let filename = args[2];
| ^^^^^^^
| |
| cannot move out of borrowed content
| help: consider borrowing here: `&args[2]`
error[E0515]: cannot return value referencing local variable `filename`
--> src/main.rs:20:12
|
20 | return (&query, &filename)
| ^^^^^^^^^---------^
| | |
| | `filename` is borrowed here
| returns a value referencing data owned by the current function
error[E0515]: cannot return value referencing local variable `query`
--> src/main.rs:20:12
|
20 | return (&query, &filename)
| ^------^^^^^^^^^^^^
| ||
| |`query` is borrowed here
| returns a value referencing data owned by the current function
No clue what is going on or why it does not work, I copied the example straight from the tutorial as well.