【发布时间】:2016-03-16 06:11:42
【问题描述】:
出于学习目的,我想将一些代码从 C 迁移到 Rust,并使我的学习库更加多语言。
问题是我知道有一种方法可以将 C 库集成到 Rust。这样我可以在 Rust 中使用 calloc 来允许创建我的数组,并在运行时指定范围。
但是我不想在这里使用calloc - 我想看看 Rust 的方式。但我也真的不想使用vec!;我之前遇到过一些愚蠢的问题,所以我还不想使用它。
代码如下:
pub struct Canvas {
width: usize,
height: usize,
array: [char], // I want to declare its type but not its size yet
}
impl Canvas{
pub fn new (&self, width: usize, height: usize) -> Canvas {
Canvas {
width: width,
height: height,
array: calloc(width, height), // alternative to calloc ? }
}
}
我希望我的问题对于 Rust 的代码方式仍然是惯用的。
【问题讨论】: