结构引用参数
pub struct Config<'a> {
pub uri: String,
pub file_name: &'a str,
pub thread_size: usize,
pub path: String,
pub scale: &'a str,
}
async fn download**<'a>**(config: & Config<''a> , client: Client<HttpsConnector>) -> Result {
let response = client.get(config.uri.parse()?).await?;
println!(“reponse is ok!”);
// let dir = &();
// println!(“dir:{}”, &dir);
write_file(response, (config.path.clone() +config.file_name.clone()).as_str()).await?;
println!("write_file is ok!");
Ok(())
}
struct就是 **Config<''a>**定义的,所以&引用时就得& **Config<''a>**,所以,在函名后<'a>,表示: **Config<''a>**的‘'a与函名后的'a sane long,函名后的'a与结构定义在同一scope,这样,引用的'a longer or long as 定义的'a.
config.path.clone()必须克隆, +config.file_name.clone()可以不.as_st。
对于必须move进函数后在函数外还要重复使用,就在函数外先.clone(),把复件move进去;如果还要在函数内修改,就得move进去后先在函数内clone,再修改后return回去,或&mut;
对于有&的结构传进去时得函数名后、(前和结构后两处声明&<'a>。
&String相当于String.as_str(),config.file_name:String,
&(&config.file_name)[2…8]:&str:, &config.file_name:str
结构引用传入,Explicit lifetimes given in parameter types where they could be elided,删除<'a>后,又Implicit elided lifetime not allowed here。