两次返回相同选项时可以避免克隆吗?
Possible to avoid cloning when returning the same option twice?
我有一个相当奇怪的情况,我需要 return 一个 vec 的选项,既在结构中,又作为选项。
我在这里举了一个小例子,看起来很傻,但这与我在更有意义的设置中遇到的问题相同(在现实世界中,container
表示计算下一个函数的状态,而输出选项是我 api)
用户的输出
pub struct Container {
opt : Option<Vec<u8>>
}
pub fn return_option(plaintext: Vec<u8>) -> Option<Vec<u8>> {
match plaintext.len() {
l if l > 5 => None,
_ => Some(plaintext)
}
}
pub fn return_stuff(plaintext: Vec<u8>) -> (Container,Option<Vec<u8>>) {
let dummycontainer = return_option(plaintext);
return (Container{
opt : dummycontainer.clone(),
},
dummycontainer)
}
因此,return_stuff
是重要的,以免编译器抱怨 dummycontainer 是移动值。
为了解决这个问题,我克隆了这个选项,这对我来说听起来像是一种反模式。
有没有办法解决这个问题,同时仍然将选项传递到两个不同的地方?
编辑
以静态生命周期借用似乎是最佳途径。
我只是不知道如何。尝试像这样编写结构:
pub struct Container {
opt : Option<&'a [u8]>
}
不允许,也不允许:
pub fn return_stuff(plaintext: Vec<u8>) -> (Container,Option<&'a [u8]>) {
假设其他人拥有 Vec<u8>
并且您所说的一切都是严格 read-only,我们可以用切片来完成整个事情。原始值仍然可以是 Vec<u8>
,但我们绝不会将其所有权传递给此处提到的任何函数。
pub struct Container<'a> {
opt : Option<&'a [u8]>
}
pub fn return_option<'a>(plaintext: &'a [u8]) -> Option<&'a [u8]> {
match plaintext.len() {
l if l > 5 => None,
_ => Some(plaintext)
}
}
pub fn return_stuff<'a>(plaintext: &'a [u8]) -> (Container<'a>,Option<&'a [u8]>) {
let dummycontainer = return_option(plaintext);
return (
Container{
opt: dummycontainer,
},
dummycontainer,
)
}
如果你有一个向量my_vec
,你可以通过借用它来调用这些函数,如return_stuff(&my_vec[..])
。
我有一个相当奇怪的情况,我需要 return 一个 vec 的选项,既在结构中,又作为选项。
我在这里举了一个小例子,看起来很傻,但这与我在更有意义的设置中遇到的问题相同(在现实世界中,container
表示计算下一个函数的状态,而输出选项是我 api)
pub struct Container {
opt : Option<Vec<u8>>
}
pub fn return_option(plaintext: Vec<u8>) -> Option<Vec<u8>> {
match plaintext.len() {
l if l > 5 => None,
_ => Some(plaintext)
}
}
pub fn return_stuff(plaintext: Vec<u8>) -> (Container,Option<Vec<u8>>) {
let dummycontainer = return_option(plaintext);
return (Container{
opt : dummycontainer.clone(),
},
dummycontainer)
}
因此,return_stuff
是重要的,以免编译器抱怨 dummycontainer 是移动值。
为了解决这个问题,我克隆了这个选项,这对我来说听起来像是一种反模式。
有没有办法解决这个问题,同时仍然将选项传递到两个不同的地方?
编辑
以静态生命周期借用似乎是最佳途径。 我只是不知道如何。尝试像这样编写结构:
pub struct Container {
opt : Option<&'a [u8]>
}
不允许,也不允许:
pub fn return_stuff(plaintext: Vec<u8>) -> (Container,Option<&'a [u8]>) {
假设其他人拥有 Vec<u8>
并且您所说的一切都是严格 read-only,我们可以用切片来完成整个事情。原始值仍然可以是 Vec<u8>
,但我们绝不会将其所有权传递给此处提到的任何函数。
pub struct Container<'a> {
opt : Option<&'a [u8]>
}
pub fn return_option<'a>(plaintext: &'a [u8]) -> Option<&'a [u8]> {
match plaintext.len() {
l if l > 5 => None,
_ => Some(plaintext)
}
}
pub fn return_stuff<'a>(plaintext: &'a [u8]) -> (Container<'a>,Option<&'a [u8]>) {
let dummycontainer = return_option(plaintext);
return (
Container{
opt: dummycontainer,
},
dummycontainer,
)
}
如果你有一个向量my_vec
,你可以通过借用它来调用这些函数,如return_stuff(&my_vec[..])
。