面试题答案
一键面试// 请在此处补充结构体定义及生命周期标注
struct Container<'a> {
// 补充成员变量声明及生命周期标注
a: &'a str,
b: &'a str,
}
// 请在此处补充方法定义及生命周期标注
impl<'a> Container<'a> {
fn combine(&self) -> &'a str {
// 实现字符串连接并返回切片
let mut result = String::new();
result.push_str(self.a);
result.push_str(self.b);
&result[..]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_combine() {
let container = Container {
a: "Hello, ",
b: "world!",
};
let combined = container.combine();
assert_eq!(combined, "Hello, world!");
}
}