pub struct Rule { /* private fields */ }
Expand description
A representation of a rule of Life-like cellular automata.
The following operations are supported:
- Constructing from a pair of truth tables
- Parsing a string into a value of this type, e.g.,
"B3/S23"
. The following notations are supported, see Rulestring:- The birth/survival notation (e.g.,
"B3/S23"
). Lowercase'b'
or's'
are also allowed in the notation instead of'B'
or'S'
- S/B notation (e.g.,
"23/3"
)
- The birth/survival notation (e.g.,
- Determining whether a new cell will be born from the specified number of alive neighbors
- Determining whether a cell surrounded by the specified number of alive neighbors will survive
- Converting into a
String
value, e.g.,"B3/S23"
. This operation only supports the birth/survival notation
§Examples
use life_backend::Rule;
let rule = "B3/S23".parse::<Rule>()?;
for i in 0..=8 {
assert_eq!(rule.is_born(i), [3].iter().any(|&x| x == i));
assert_eq!(rule.is_survive(i), [2, 3].iter().any(|&x| x == i));
}
assert_eq!(format!("{rule}"), "B3/S23");
Implementations§
Source§impl Rule
impl Rule
Sourcepub const fn new(birth: &[bool; 9], survival: &[bool; 9]) -> Self
pub const fn new(birth: &[bool; 9], survival: &[bool; 9]) -> Self
Creates a new rule based on the specified pair of truth tables.
§Examples
use life_backend::Rule;
let rule = Rule::new(
&[false, false, false, true, false, false, false, false, false],
&[false, false, true, true, false, false, false, false, false],
);
let b = [3];
let s = [2, 3];
for i in 0..=8 {
assert_eq!(rule.is_born(i), b.iter().any(|&x| x == i));
assert_eq!(rule.is_survive(i), s.iter().any(|&x| x == i));
}
Sourcepub const fn is_survive(&self, count: usize) -> bool
pub const fn is_survive(&self, count: usize) -> bool
Returns whether a cell surrounded by the specified number of alive neighbors will survive.
§Panics
Panics if the argument count
is greater than 8.
§Examples
use life_backend::Rule;
let rule = Rule::conways_life();
let s = [2, 3];
for i in 0..=8 {
assert_eq!(rule.is_survive(i), s.iter().any(|&x| x == i));
}
Sourcepub const fn conways_life() -> Self
pub const fn conways_life() -> Self
Returns the rule of Conway’s Game of Life.
§Examples
use life_backend::Rule;
let rule = Rule::conways_life();
let b = [3];
let s = [2, 3];
for i in 0..=8 {
assert_eq!(rule.is_born(i), b.iter().any(|&x| x == i));
assert_eq!(rule.is_survive(i), s.iter().any(|&x| x == i));
}
Trait Implementations§
impl Eq for Rule
impl StructuralPartialEq for Rule
Auto Trait Implementations§
impl Freeze for Rule
impl RefUnwindSafe for Rule
impl Send for Rule
impl Sync for Rule
impl Unpin for Rule
impl UnwindSafe for Rule
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more