如何部分初始化ArrayVec?

时间:2018-12-14 16:27:46

标签: syntax rust initialization

我需要固定最大大小的结构,因此显而易见的选择似乎是arrayvec crate。但是,当def set_brightness(brightness): if int(brightness) > 15: raise TypeError("Need int 0 < and > 15") elif int(brightness) < 0: raise TypeError("Need int 0 < and > 15") with open("/sys/devices/pci0000:00/0000:00:02.0/backlight/acpi_video0/brightness","w") as bright: bright.write(str(brightness)) bright.close() set_brightness(0) #Brightness 0-15 是一个结构的成员时,我就陷入困境,该结构以后需要部分初始化:

ArrayVec

我想初始化use arrayvec::ArrayVec; // 0.4.7 #[derive(Debug)] struct Test { member_one: Option<u32>, member_two: ArrayVec<[u16; 5]>, } pub fn main() { let mut test = Test { member_one: Some(45678), member_two: [1, 2, 3], // <- What to do here to initialise only 3 elements? }; print!("{:?}", test); } 的前三个元素,因为它完全能够容纳从零到5的任意数量的元素(在我的示例中),但是我不知道该怎么做它。

2 个答案:

答案 0 :(得分:2)

您可以从迭代器中收集到filteredLinks () { return this.links.filter((link) => { return link.items.some((item) => { return item.name.toLowerCase().includes(this.searchQuery.toLowerCase()) }) }) } 中:

ArrayVec

答案 1 :(得分:1)

ArrayVec不提供单步执行此操作的方法。而是创建ArrayVec,然后以任何添加值的方式向其中添加值:

let mut member_two = ArrayVec::new();
member_two.extend([1, 2, 3].iter().cloned());
let test = Test {
    member_one: Some(45678),
    member_two, 
};