如何在 Rust 的构造函数中创建数组
How to create an array within a constructor in Rust
出于学习目的,我想将一些代码从 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 代码方式。
首先,我深深地怀疑你不想char
;我假设你想要 "array of bytes",在这种情况下你想要 u8
.
其次,你不能真的像这样使用[u8]
。我不打算讨论 为什么 因为那只会使答案偏离轨道。现在:如果您看到 [T]
而 不是 在某种引用或指针后面,那是 可能 一个错误。
最后,这就是 Vec
的目的;用它。你说你不想使用它,但没有说明原因。 Vec
是 你如何在 Rust 中分配一个动态大小的数组。如果您尝试分配与 C 中完全相同的结构兼容的结构,那么问题会发生很大变化,您应该弄清楚。
假设您想要的是 "Rust equivalent" 在 C:
中完成
pub struct Canvas {
width: usize,
height: usize,
array: Vec<u8>,
}
impl Canvas {
pub fn new(width: usize, height: usize) -> Canvas {
Canvas {
width: width,
height: height,
array: vec![0; width*height],
}
}
}
i want to access that array with coordinates style
是这样的吗?
pub struct Canvas {
width: usize,
height: usize,
data: Vec<u8>,
}
impl Canvas {
pub fn new(width: usize, height: usize) -> Canvas {
Canvas {
width: width,
height: height,
data: vec![0; width*height],
}
}
fn coords_to_index(&self, x: usize, y: usize) -> Result<usize, &'static str> {
if x<self.width && y<self.height {
Ok(x+y*self.width)
} else {
Err("Coordinates are out of bounds")
}
}
pub fn get(&self, x: usize, y: usize) -> Result<u8, &'static str> {
self.coords_to_index(x, y).map(|index| self.data[index])
}
pub fn set(&mut self, x: usize, y: usize, new_value: u8) -> Result<(), &'static str>{
self.coords_to_index(x, y).map(|index| {self.data[index]=new_value;})
}
}
fn main() {
let mut canvas = Canvas::new(100, 100);
println!("{:?}", canvas.get(50, 50)); // Ok(0)
println!("{:?}", canvas.get(101, 50)); // Err("Coordinates are out of bounds")
println!("{:?}", canvas.set(50, 50, 128)); // Ok(())
println!("{:?}", canvas.set(101, 50, 128)); // Err("Coordinates are out of bounds")
println!("{:?}", canvas.get(50, 50)); // Ok(128)
}
出于学习目的,我想将一些代码从 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 代码方式。
首先,我深深地怀疑你不想char
;我假设你想要 "array of bytes",在这种情况下你想要 u8
.
其次,你不能真的像这样使用[u8]
。我不打算讨论 为什么 因为那只会使答案偏离轨道。现在:如果您看到 [T]
而 不是 在某种引用或指针后面,那是 可能 一个错误。
最后,这就是 Vec
的目的;用它。你说你不想使用它,但没有说明原因。 Vec
是 你如何在 Rust 中分配一个动态大小的数组。如果您尝试分配与 C 中完全相同的结构兼容的结构,那么问题会发生很大变化,您应该弄清楚。
假设您想要的是 "Rust equivalent" 在 C:
中完成pub struct Canvas {
width: usize,
height: usize,
array: Vec<u8>,
}
impl Canvas {
pub fn new(width: usize, height: usize) -> Canvas {
Canvas {
width: width,
height: height,
array: vec![0; width*height],
}
}
}
i want to access that array with coordinates style
是这样的吗?
pub struct Canvas {
width: usize,
height: usize,
data: Vec<u8>,
}
impl Canvas {
pub fn new(width: usize, height: usize) -> Canvas {
Canvas {
width: width,
height: height,
data: vec![0; width*height],
}
}
fn coords_to_index(&self, x: usize, y: usize) -> Result<usize, &'static str> {
if x<self.width && y<self.height {
Ok(x+y*self.width)
} else {
Err("Coordinates are out of bounds")
}
}
pub fn get(&self, x: usize, y: usize) -> Result<u8, &'static str> {
self.coords_to_index(x, y).map(|index| self.data[index])
}
pub fn set(&mut self, x: usize, y: usize, new_value: u8) -> Result<(), &'static str>{
self.coords_to_index(x, y).map(|index| {self.data[index]=new_value;})
}
}
fn main() {
let mut canvas = Canvas::new(100, 100);
println!("{:?}", canvas.get(50, 50)); // Ok(0)
println!("{:?}", canvas.get(101, 50)); // Err("Coordinates are out of bounds")
println!("{:?}", canvas.set(50, 50, 128)); // Ok(())
println!("{:?}", canvas.set(101, 50, 128)); // Err("Coordinates are out of bounds")
println!("{:?}", canvas.get(50, 50)); // Ok(128)
}