arr
implements IntoIterator trait (into_iter) arr.into_iter()
can be replaced by arr
.
let mut n_used: Array1<usize> = Array::zeros(CHANNELS);
let grid: Array1<bool> = Array::default(CHANNELS);
// the trait bound `ndarray::ArrayBase<ndarray::OwnedRepr<usize>, ndarray::Dim<[usize; 1]>>: std::convert::From<ndarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<[usize; 1]>>>` is not satisfied (the trait `std::convert::From<ndarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<[usize; 1]>>>` is not implemented for `ndarray::ArrayBase<ndarray::OwnedRepr<usize>, ndarray::Dim<[usize; 1]>>`) (rustcargo)
let g: Array1<usize> = grid.into();
n_used = n_used + g;
Read
and wraps it in a Read
or BufRead
that can be sent to another thread, still reading the inner Read
in the first thread. I have an XML reader wrapping a Bzip2 reader using 100% CPU on one thread, and I would like to try running them in different threads.
pub fn from_u16(raw: u16) > Option<Cmd> {
match raw {
0x01  0x02  0x03  0x04  0x10  0x45  0x47  0x4A  0x5A  0x78 => unsafe {
Some(std::mem::transmute(raw))
},
_ => None,
}
}
error[E0512]: transmute called with types of different sizes
> src/lib.rs:41:22

41  Some(std::mem::transmute(raw))
 ^^^^^^^^^^^^^^^^^^^

= note: source type: u16 (16 bits)
= note: target type: Cmd (8 bits)
Cmd
is the enum ?
u16 as Cmd
?
as
can cast an enum to its descriminant, but not vice versa as that operation can fail
But in this example, values are explicitly matched.
And what if I made mistake in one number or forgot one number? Better solution is crate linked by @ozkriff this do exactly what I want!
enum
.
Note: this trait must not fail. If the conversion can fail, use TryFrom or a dedicated method which returns an Option<T> or a Result<T, E>.
is there a simpler way to do:
let (r, c) = (neigh[0], neigh[1])
provided neigh
is a ndarray
of length 2?
ndarray
is even sliceable
let [a, b] = [2i32; 2];
Array1<usize>
as an example.
&array![1, 2]
Index
trait
neigh = &array![1,2]
then I expect them to be 1
and 2
respectively, i.e. as it would be in python if you did a, b = [1, 2]
a, b = np.array([1, 2])
val NDArray(a,b,c,d,e,f,g,h,i) = NDArray(1,2)
if you know what youre doing :)
Is there a better way to do the last line of:
let I = 10;
let mut freps: Array1<usize> = Array::ones((I));
let diff: isize = 1;
let i = 1;
freps[[i]] = (freps[[i]] as isize + diff) as usize;
Assuming to know that adding diff will always result in a positive value?
diff = 1
and then diff
?
if diff = 1 {freps[[i]] = 1} else {freps[[i]] += 1}
/// Given a grid, its feature representation frep,
/// and a set of actions specified by cell, event type and a list of channels,
/// derive feature representations for the afterstates of grid
fn incremental_freps(
grid: &Grid,
frep: &Frep,
cell: &Cell,
ce_type: &CEType,
chs: &[usize],
) > Freps {
let (r1, c1) = (cell.row, cell.col);
let neighs4 = neighbors(4, r1, c1, false);
let neighs2 = neighbors(2, r1, c1, true);
let mut freps = Array::zeros((chs.len(), ROWS, COLS, CHANNELS + 1));
freps.assign(&frep);
let mut n_used_neighs_diff: isize = 1;
let mut n_elig_self_diff: isize = 1;
if *ce_type == CEType::END {
n_used_neighs_diff = 1;
n_elig_self_diff = 1;
}
for (i, ch) in chs.into_iter().enumerate() {
for neigh in neighs4.outer_iter() {
freps[[i, neigh[0], neigh[1], *ch]] =
(freps[[i, neigh[0], neigh[1], *ch]] as isize + n_used_neighs_diff) as usize;
}
for neigh_a in neighs2.outer_iter() {
let (r2, c2) = (neigh_a[0], neigh_a[1]);
let neighs = neighbors(2, r2, c2, false);
let mut not_eligible = grid[[r2, c2, *ch]];
for neigh_b in neighs.outer_iter() {
not_eligible = grid[[neigh_b[0], neigh_b[1], *ch]];
}
if !not_eligible {
freps[[i, neigh_a[0], neigh_a[1], CHANNELS]] =
(freps[[i, neigh_a[0], neigh_a[1], CHANNELS]] as isize + n_elig_self_diff)
as usize;
}
}
}
freps
}
*freps[[i, neigh[0]]]...
*freps[[i, neigh[0], neigh[1], *ch]] += n_used_neighs_diff
? => usize cannot be dereferenced
let (n_used_neighs_diff, n_elig_self_diff) = if *ce_type == CEType::END {(1, 1)} else {(1, 1)};
1isize
5f32
= 5.0
= :f32 = 5;
trait IncDec {
fn i_d(&mut self, i: bool);
}
impl IncDec for usize {
fn i_d(&mut self, i: bool) {
if i {
*self += 1;
} else {
*self = 1;
}
}
}
fn main() {
let mut u = 15usize;
u.i_d(true);
println!("{}", u);
u.i_d(false);
println!("{}", u);
}
Say I have
struct Event {
a: i32,
b: i32
}
Is there a way to auto derive Ord
for Event
based on b
?