網(wǎng)站首頁 編程語言 正文
最近在用rust 寫一個redis的數(shù)據(jù)校驗(yàn)工具。redis-rs中具備 redis::ConnectionLike trait,借助它可以較好的來抽象校驗(yàn)過程。在開發(fā)中,不免要定義struct 中的某些元素為 trait object,從而帶來一些rust語言中的生命周期問題。
本文不具體討論 redis的數(shù)據(jù)校驗(yàn)過程,通過一個簡單的例子來聊聊 struct 中 trait object 元素的生命周期問題。
首先來定義一個 base trait,該 trait 中只包含一個函數(shù),返回String類型。
pub trait Base {
fn say(&self) -> String;
}
接下來,定義兩個實(shí)現(xiàn)了 Base trait 的 struct AFromBase 和 BFromBase
pub struct AFromBase {
content: String,
}
impl Base for AFromBase {
fn say(&self) -> String {
self.content.clone()
}
}
pub struct BFromBase {
text: String,
}
impl Base for BFromBase {
fn say(&self) -> String {
self.text.clone()
}
}
接下來,定義一個struct 包含兩個 Base trait 的 trait object ,然后實(shí)現(xiàn)一個函數(shù)是 say 函數(shù)輸出的字符串的拼接結(jié)果.
按照其他沒有生命周期語言的編寫習(xí)慣,直覺上這么寫
pub struct AddTowBase {
a: &mut dyn Base,
b: &mut dyn Base,
}
impl AddTowBase {
fn add(&self) -> String {
let result = self.a.say() + &self.b.say();
result
}
}
最后,搞個main函數(shù)驗(yàn)證一下。
完整代碼如下
pub trait Base {
fn say(&self) -> String;
}
pub struct AFromBase {
content: String,
}
impl Base for AFromBase {
fn say(&self) -> String {
self.content.clone()
}
}
pub struct BFromBase {
text: String,
}
impl Base for BFromBase {
fn say(&self) -> String {
self.text.clone()
}
}
pub struct AddTowBase {
a: &mut dyn Base,
b: &mut dyn Base,
}
impl<'a> AddTowBase<'a> {
fn add(&self) -> String {
let result = self.a.say() + &self.b.say();
result
}
}
fn main() {
let mut a = AFromBase {
content: "baseA".to_string(),
};
let mut b = BFromBase {
text: "baseB".to_string(),
};
let addtow = AddTowBase {
a: &mut a,
b: &mut b,
};
let r = addtow.add();
println!("{}", r);
}
很遺憾,以上代碼是不能編譯通過的,編譯時(shí)報(bào)如下錯誤
error[E0106]: missing lifetime specifier
--> examples/lifetimeinstruct.rs:26:8
|
26 | a: &mut dyn Base,
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
25 ~ pub struct AddTowBase<'a> {
26 ~ a: &'a mut dyn Base,
|
error[E0106]: missing lifetime specifier
--> examples/lifetimeinstruct.rs:27:8
|
27 | b: &mut dyn Base,
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
25 ~ pub struct AddTowBase<'a> {
26 | a: &mut dyn Base,
27 ~ b: &'a mut dyn Base,
|
For more information about this error, try `rustc --explain E0106`.
error: could not compile `wenpan-rust` due to 2 previous errors
編譯器給出的提示很明確,要在 trait object 上添加生命周期參數(shù),確保 struct 和他的 trait object 元素在同一生命周期,避免懸垂指針。
我們按照編譯器的提示修改代碼
pub struct AddTowBase<'a> {
a: &'a mut dyn Base,
b: &'a mut dyn Base,
}
impl<'a> AddTowBase<'a> {
fn add(self) -> String {
let result = self.a.say() + &self.b.say();
result
}
}
代碼順利通過編譯。
rust 的生命周期保證了內(nèi)存的安全性,同時(shí)也增加了開發(fā)者的心智負(fù)擔(dān)。是在上線之前多費(fèi)心思寫代碼,還是在上線以后忙忙活活查問題,這是個 trade off 問題。俗話講:"背著抱著,一樣沉".我本人還是傾向于把問題控制在上線之前,少折騰用戶。
原文鏈接:https://www.cnblogs.com/Jcloud/p/16768365.html
相關(guān)推薦
- 2023-08-28 vscode里面報(bào):‘xxx‘ is assigned a value but never used
- 2022-04-24 Android為View添加拖放效果的方法實(shí)例_Android
- 2022-07-14 Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫圓_Android
- 2022-07-07 C++實(shí)現(xiàn)數(shù)組中元素組合出最大值_C 語言
- 2022-10-01 一文詳解C#?Chart控件_C#教程
- 2022-05-16 C#?CM框架實(shí)現(xiàn)多頁面管理的實(shí)例代碼_C#教程
- 2022-05-04 EF使用數(shù)據(jù)注解特性創(chuàng)建表結(jié)構(gòu)_實(shí)用技巧
- 2022-11-01 Python如何使用qrcode生成指定內(nèi)容的二維碼并在GUI界面顯示_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支