0

编译

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)
}

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)
}

对于非工作示例,我收到以下错误:

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 `()`

当宏在 return 语句中时,是否会发生一些隐含的事情?

4

0 回答 0