I'm using restson to make REST calls.
Reston allows you to make a Vec
of query params to pass to the REST call. This is the example they provide in their documentation:
let query = vec![("a","2"), ("b","abcd")];
let data: HttpBinAnything = client.get_with(1234, &query).unwrap();
In my case I need to pass a parameter to deal with pagination:
- On the first request no parameter needs to be supplied.
- The response body contains an "after" field
- Requests to get the next page need to supply the after field as a query parameter.
If I have a method like this it compiles:
fn fetch_page(client: &mut RestClient, after: Option<String>) -> Result<Page, Error> {
match after {
Some(s) => {
let query = vec![("after", s.as_str())];
client.get_with(&query)
}
None => {
let query = vec![];
client.get_with(&query)
}
}
}
If I try to use anything other than pattern matching it fails. For example the following code:
fn fetch_page(client: &mut RestClient, after: Option<String>) -> Result<Page, Error> {
let query = after.map_or(vec![], |s| vec![("after", s.as_str())] );
client.get_with(&query)
}
Results in the following error:
error[E0515]: cannot return value referencing function parameter `s`
--> src/reddit_scrape.rs:53:42
|
53 | let query = after.map_or(vec![], |s| vec![("after", s.as_str())] );
| ^^^^^^^^^^^^^^^-^^^^^^^^^^^
| | |
| | `s` is borrowed here
| returns a value referencing data owned by the current function