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