pub struct RewriteRules(pub HashMap<char, String>);Expand description
Describes the rewriting rules that will be iterated.
§Example
use std::collections::HashMap;
use l_system_fractals::rules::RewriteRules;
let cantor = RewriteRules(
HashMap::from([
('=', "=.=".into()),
('.', "...".into())
])
);
let mut dust = cantor.iterate("=".to_string());
assert_eq!(&dust, "=.=");
dust = cantor.iterate(dust);
assert_eq!(&dust, "=.=...=.=");
dust = cantor.iterate(dust);
assert_eq!(&dust, "=.=...=.=.........=.=...=.=");Tuple Fields§
§0: HashMap<char, String>Implementations§
Source§impl RewriteRules
impl RewriteRules
Sourcepub fn iterate(&self, start: String) -> String
pub fn iterate(&self, start: String) -> String
Applies the rules once to a string.
§Example
use std::collections::HashMap;
use l_system_fractals::rules::RewriteRules;
let r = RewriteRules(
HashMap::from([
('A', "AB".into()),
('B', "CA".into()),
('C', "AA".into())
])
);
assert_eq!(r.iterate("A".to_string()), "AB".to_string());
assert_eq!(r.iterate("ABC".to_string()), "ABCAAA".to_string());
assert_eq!(r.iterate("ACX".to_string()), "ABAAX".to_string());Sourcepub fn repeat_iterate(&self, start: String, num_iter: usize) -> String
pub fn repeat_iterate(&self, start: String, num_iter: usize) -> String
Applies the rule the specified number of times.
§Example
use std::collections::HashMap;
use l_system_fractals::rules::RewriteRules;
let cantor = RewriteRules(
HashMap::from([
('=', "=.=".into()),
('.', "...".into())
])
);
assert_eq!(cantor.repeat_iterate("=".to_string(), 0), "=".to_string());
assert_eq!(cantor.repeat_iterate("=".to_string(), 1), "=.=".to_string());
assert_eq!(cantor.repeat_iterate("=".to_string(), 2), "=.=...=.=".to_string());
assert_eq!(
cantor.repeat_iterate("=".to_string(), 3),
"=.=...=.=.........=.=...=.=".to_string()
);Sourcepub fn iterations_up_to(
&self,
start: String,
num_iter: usize,
) -> HashMap<usize, String>
pub fn iterations_up_to( &self, start: String, num_iter: usize, ) -> HashMap<usize, String>
Calculates the first num_iter iterations of the rule applied to a given string.
§Example
use std::collections::HashMap;
use l_system_fractals::rules::RewriteRules;
let cantor = RewriteRules(
HashMap::from([
('=', "=.=".into()),
('.', "...".into())
])
);
let output = cantor.iterations_up_to("=".to_string(), 3);
assert_eq!(output.get(&0).unwrap(), "=");
assert_eq!(output.get(&1).unwrap(), "=.=");
assert_eq!(output.get(&2).unwrap(), "=.=...=.=");
assert_eq!(output.get(&3).unwrap(), "=.=...=.=.........=.=...=.=");Trait Implementations§
Source§impl Clone for RewriteRules
impl Clone for RewriteRules
Source§fn clone(&self) -> RewriteRules
fn clone(&self) -> RewriteRules
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for RewriteRules
impl Debug for RewriteRules
Source§impl PartialEq for RewriteRules
impl PartialEq for RewriteRules
impl Eq for RewriteRules
impl StructuralPartialEq for RewriteRules
Auto Trait Implementations§
impl Freeze for RewriteRules
impl RefUnwindSafe for RewriteRules
impl Send for RewriteRules
impl Sync for RewriteRules
impl Unpin for RewriteRules
impl UnwindSafe for RewriteRules
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