编辑:这个问题与 Rust >= 1.47的版本无关,因为“const generics”已经开始实现。
我正在尝试在 Rust 中实现一个数独求解器以用于学习目的。我正在尝试创建一个具有固定大小 (81) Cells 数组(即Copy)的板,但我似乎无法让它工作。我可以创建一行 9 ,Cell所以我想我遇到了这样的问题:对于TryFrom这种转换,只有最多 32 个的泛型。
Cell现在看起来像这样:
#[derive(Debug, Default, Clone, Copy)]
struct Cell {
row: u8,
column: u8,
}
Run Code Online (Sandbox Code Playgroud)
这有效:
use std::convert::TryInto;
fn main() {
let cells: Vec<Cell> = std::iter::repeat(0)
.zip(0..9u8)
.map(|(row, column)| Cell { row, column} )
.collect();
let c: Box<[Cell; 9]> = cells.into_boxed_slice().try_into().unwrap();
println!("{:#?}", c);
}
Run Code Online (Sandbox Code Playgroud)
但这并没有:
use std::convert::TryInto;
fn main() {
let cells: Vec<Cell> = (0..9u8).into_iter()
.flat_map(|x| {
std::iter::repeat(x)
.zip(0..9u8)
})
.map(|(row, column)| Cell { row, column} )
.collect();
let c: Box<[Cell; 81]> = cells.into_boxed_slice().try_into().unwrap();
println!("{:#?}", c);
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码std作为指南:
impl TryFrom<Box<[Cell]>> for Box<[Cell; 81]> {
type Error = Box<[Cell]>;
fn try_from(boxed_slice: Box<[Cell]>) -> Result<Self, Self::Error> {
if boxed_slice.len() == 81 {
Ok(unsafe { Box::from_raw(Box::into_raw(boxed_slice) as *mut [Cell; 91]) })
} else {
Err(boxed_slice)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这会遇到一个错误,conflicting implementations of trait我认为这是有道理的。
我知道我可以使用 aVec或做类似的事情[[Cell; 9]; 9],但我真的很想了解发生了什么。在试图解决这个问题时,我看到了许多类似的问题,人们试图使用未实现的类型Copy,这就是问题所在,但这里的情况并非如此,我不知道如何使这项工作有效。
该特征FromIterator未针对切片实现,您可以将其包装在类型中并自己实现:
use core::iter::FromIterator;
#[derive(Debug, Default, Clone, Copy)]
struct Cell {
row: u8,
column: u8,
}
#[derive(Debug)]
struct Sudoku(Box<[Cell]>);
impl FromIterator<Cell> for Sudoku {
fn from_iter<I: IntoIterator<Item=Cell>>(iter: I) -> Self {
let mut v = Vec::new();
for cell in iter {
v.push(cell)
}
Sudoku(v.into_boxed_slice())
}
}
fn main() {
let cells: Sudoku = (0..9u8).into_iter()
.flat_map(|x| {
std::iter::repeat(x)
.zip(0..9u8)
})
.map(|(row, column)| Cell { row, column} )
.collect();
println!("{:#?}", cells);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
您还可以针对数组的特定大小来实现它。对于数独情况应该没问题,但一般来说您希望事情以更通用的方式工作。您可以编写一个宏来编写任何给定大小的实现,而不是固定大小。
例子:
use core::iter::FromIterator;
use std::fmt;
#[derive(Clone, Copy)]
struct Cell {
row: u8,
column: u8,
}
impl fmt::Display for Cell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Cell: ({}, {})", self.row, self.column)
}
}
struct Sudoku([Cell; 81]);
impl fmt::Display for Sudoku {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for cell in self.0.iter() {
write!(f, "{}\n", cell)?;
}
Ok(())
}
}
impl FromIterator<Cell> for Sudoku {
fn from_iter<I: IntoIterator<Item=Cell>>(iter: I) -> Self {
let mut v = [Cell {row: 0, column: 0}; 81];
for (i, cell) in (0..81).zip(iter) {
v[i] = cell;
}
Sudoku(v)
}
}
fn main() {
let cells: Sudoku = (0..9u8).into_iter()
.flat_map(|x| {
std::iter::repeat(x)
.zip(0..9u8)
})
.map(|(row, column)| Cell { row, column} )
.collect();
println!("{}", cells);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1161 次 |
| 最近记录: |