为什么格式化!不能在return语句中使用?

时间:2019-07-08 14:22:05

标签: rust rust-macros

编译

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语句中是否发生了隐式的事情?

0 个答案:

没有答案