我想将一些代码从C迁移到Rust以用于学习目的,并使我的学习库更加多语言化。如何在Rust中构造函数中创建数组
问题是我知道有一种方法可以将C库集成到Rust中。这样,我可能在Rust中使用calloc
允许创建我的数组与运行时指定的范围。
但是我不想在这里使用calloc
- 我想看看锈的方式。但我真的不想用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 ? }
}
}
我希望我的问题仍然是惯用的以代码的方式锈。
正是我在找的东西! – xetra11
对于“内置”的感觉,请注意[Index](https://doc.rust-lang.org/std/ops/trait.Index.html)和[IndexMut](https:// doc.rust浪。org/std/ops/trait.IndexMut.html),可以实现'(usize,usize)'。他们不能报告错误,所以他们会补充你在这里写的方法,然后你可以使用'canvas [(50,50)] = 128'。 –