我的类型为Clock
:
#[derive(Debug, PartialEq)]
pub struct Clock {
hours: i32,
minutes: i32,
}
为此实现了一些特征。例如:
#[allow(clippy::match_bool)]
impl fmt::Display for Clock {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let tmp = match (self.hours < 10, self.minutes < 10) {
(false, false) => ("", self.hours, "", self.minutes),
(false, true) => ("", self.hours, "0", self.minutes),
(true, false) => ("0", self.hours, "", self.minutes),
(true, true) => ("0", self.hours, "0", self.minutes),
};
write!(
formatter,
"{}",
format!("{}{}:{}{}", tmp.0, tmp.1, tmp.2, tmp.3)
)
}
}
我要实现String::from(Clock::new(...))
。我该怎么办?
我尝试过:
impl convert::From<Clock> for String {
fn from(clock: Clock) -> String {
clock.to_string()
}
}
答案 0 :(得分:-1)
有效的解决方案:
True