0

This compiles:

fn fmt(url: &str) -> String {
    const base_url: &'static str = "https://api.github.com/";
    let req_url = format!("{}{}", &base_url, &url);
    req_url
}

fn main() {
    let url = fmt("bla/bla");
    println!("{}", url)
}

This doesn't

fn fmt(url: &str) -> String {
    const base_url: &'static str = "https://api.github.com/";
    format!("{}{}", &base_url, &url);
}

fn main() {
    let url = fmt("bla/bla");
    println!("{}", url)
}

For the non-working example I get the following error:

error[E0308]: mismatched types
 --> src/main.rs:1:22
  |
1 | fn fmt(url: &str) -> String {
  |    ---               ^^^^^^ expected struct `std::string::String`, found ()
  |    |
  |    this function's body doesn't return
2 |     const base_url: &'static str = "https://api.github.com/{request_url}";
3 |     format!("{}{}", &base_url, &url);
  |                                     - help: consider removing this semicolon
  |
  = note: expected type `std::string::String`
             found type `()`

Is there something implicit happening when a macro is in a return statement?

Keeper Hood
  • 594
  • 5
  • 17

0 Answers0