结构引用参数

结构引用参数
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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值