【发布时间】:2020-07-18 08:02:47
【问题描述】:
如何在rust中创建字节切片常量,如下?
// This does not compile, but just to show my intention.
pub const MyConst: &'static [u8; 256] = b"abcdef" + [0u8; 250];
// Basically, the value is b"abcdef00000...", the `000...` appended at the end
// are a series of byte 0, not character 0.
【问题讨论】:
-
您可以使用 const 函数执行此操作,或者手动编写常量:
b"abcdef\0\0\0\0\0\0\0\0..."。更多上下文将有助于您了解为什么需要它。 -
我不确定是否有 const 函数可以与
[u8; 256]一起使用。我认为你必须手写这个。如果它可以是static而不是const,那么还有更多选项(例如lazy_static)。
标签: rust