(pat: P) Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html Checks if the string starts with the given pattern. ```APIDOC ## pub fn starts_with
(&self, pat: P) -> bool ### Description Returns `true` if the given pattern matches a prefix of this string slice. ``` -------------------------------- ### Example Usage of Roster Import Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/import/fn.resolve.html Demonstrates importing a roster using Listforge format and the embedded dataset. ```rust use wh40kdc::Dataset; use wh40kdc::import::{import_roster, decode_listforge, RosterFormat}; let payload = decode_listforge(r#"{ "name": "Demo", "roster": { "name": "Demo", "forces": [] } }"#).unwrap(); let roster = import_roster(&payload, Dataset::embedded()).unwrap(); assert_eq!(roster.source.format, RosterFormat::Listforge); ``` -------------------------------- ### Check slice prefix with starts_with Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Determines if a slice starts with a given prefix. ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` -------------------------------- ### Partitioning a slice Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Examples demonstrating the use of partition_point to find the index where a predicate changes from true to false. ```rust assert_eq!(i, 4); assert!(v[..i].iter().all(|&x| x < 5)); assert!(v[i..].iter().all(|&x| !(x < 5))); ``` ```rust let a = [2, 4, 8]; assert_eq!(a.partition_point(|x| x < &100), a.len()); let a: [i32; 0] = []; assert_eq!(a.partition_point(|x| x < &100), 0); ``` ```rust let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let num = 42; let idx = s.partition_point(|&x| x <= num); s.insert(idx, num); assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); ``` -------------------------------- ### Retrieve match indices in strings Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.RiskRewardEffectRiskTest.html Use match_indices or rmatch_indices to get both the start index and the matched content. ```rust let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect(); assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); let v: Vec<_> = "1abcabc2".match_indices("abc").collect(); assert_eq!(v, [(1, "abc"), (4, "abc")]); let v: Vec<_> = "ababa".match_indices("aba").collect(); assert_eq!(v, [(0, "aba")]); // only the first `aba` ``` ```rust let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect(); assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]); let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect(); assert_eq!(v, [(4, "abc"), (1, "abc")]); let v: Vec<_> = "ababa".rmatch_indices("aba").collect(); assert_eq!(v, [(2, "aba")]); // only the last `aba` ``` -------------------------------- ### Check if string starts with a pattern Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html Returns true if the pattern matches the prefix of the string. ```rust let bananas = "bananas"; assert!(bananas.starts_with("bana")); assert!(!bananas.starts_with("nana")); ``` ```rust let bananas = "bananas"; // Note that both of these assert successfully. assert!(bananas.starts_with(&['b', 'a', 'n', 'a'])); assert!(bananas.starts_with(&['a', 'b', 'c', 'd'])); ``` -------------------------------- ### Check prefix and suffix Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.DeploymentPatternSource.html Verifies if a string slice starts or ends with a given pattern. ```rust let bananas = "bananas"; assert!(bananas.starts_with("bana")); assert!(!bananas.starts_with("nana")); ``` ```rust let bananas = "bananas"; // Note that both of these assert successfully. assert!(bananas.starts_with(&['b', 'a', 'n', 'a'])); assert!(bananas.starts_with(&['a', 'b', 'c', 'd'])); ``` ```rust let bananas = "bananas"; assert!(bananas.ends_with("anas")); assert!(!bananas.ends_with("nana")); ``` -------------------------------- ### pub fn starts_with
(&self, pat: P) -> bool Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.MissionName.html Checks if the given pattern matches a prefix of this string slice. ```APIDOC ## pub fn starts_with
(&self, pat: P) -> bool ### Description Returns `true` if the given pattern matches a prefix of this string slice. Returns `false` if it does not. ``` -------------------------------- ### Check Vector Length Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Example of checking the length of a vector. ```rust let a = vec![1, 2, 3]; assert_eq!(a.len(), 3); ``` -------------------------------- ### Extract Vector Slice Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Example of extracting a slice from a vector. ```rust use std::io::{self, Write}; let buffer = vec![1, 2, 3, 5, 8]; io::sink().write(buffer.as_slice()).unwrap(); ``` -------------------------------- ### Get slice length Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Returns the number of elements in the slice. ```rust let a = [1, 2, 3]; assert_eq!(a.len(), 3); ``` -------------------------------- ### pub fn repeat(&self, n: usize) -> String Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.DetachmentName.html Creates a new String by repeating the original string n times. ```APIDOC ## pub fn repeat(&self, n: usize) -> String ### Description Creates a new String by repeating a string n times. ### Parameters - **n** (usize) - The number of times to repeat the string. ### Panics - Panics if the resulting capacity would overflow. ``` -------------------------------- ### Trim start of a string Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html Removes leading whitespace from a string slice. ```rust let s = " עברית "; assert!(Some('ע') == s.trim_start().chars().next()); ``` -------------------------------- ### Execute damage projection with crunch Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/cruncher/index.html Demonstrates initializing a dataset, defining an engine input with attacker and target profiles, and executing the crunch function to calculate damage stages. ```rust use wh40kdc::{Dataset, Phase}; use wh40kdc::cruncher::{ crunch, AttackProfileRef, EngineContext, EngineInput, TargetProfileRef, }; let ds = Dataset::embedded(); let weapon = ds.find_weapon("bolt-rifle").expect("bolt-rifle is bundled"); let target = ds.find_unit("intercessor-squad").expect("intercessor-squad is bundled"); let input = EngineInput { attacker: AttackProfileRef { weapon, profile_index: 0 }, target: TargetProfileRef { unit: target, profile_index: 0, model_count: None }, models_firing: 5, buffs: Vec::new(), context: EngineContext { phase: Phase::Shooting, attacker_stationary: Some(false), attacker_charged: None, within_half_range: Some(false), distance_inches: None, attacker_in_cover: None, target_in_cover: None, attacker_keywords: None, target_keywords: None, timing: None, attacker_attached: None, }, }; let out = crunch(&input, None).expect("crunch succeeds"); assert_eq!(out.stages.len(), 7); ``` -------------------------------- ### Check if Vector is Empty Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Example of checking if a vector contains no elements. ```rust let mut v = Vec::new(); assert!(v.is_empty()); v.push(1); assert!(!v.is_empty()); ``` -------------------------------- ### Get string length with len Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html Returns the length of the String in bytes. ```rust let a = String::from("foo"); assert_eq!(a.len(), 3); let fancy_f = String::from("ƒoo"); assert_eq!(fancy_f.len(), 4); assert_eq!(fancy_f.chars().count(), 3); ``` -------------------------------- ### Lookup and Filter Units and Abilities Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/data/struct.Dataset.html Demonstrates initializing the dataset, performing a fuzzy unit lookup, and filtering abilities by phase. ```rust use wh40kdc::{Dataset, Phase}; let ds = Dataset::embedded(); let kharn = ds.find_unit("Kharn").expect("Khârn is in the dataset"); let shooting_abilities: Vec<&str> = ds .abilities_of(kharn) .into_iter() .filter(|a| ds.phases_of(a).contains(&Phase::Shooting)) .map(|a| a.ability_id.as_str()) .collect(); assert_eq!(shooting_abilities, ["berzerker-frenzy"]); assert_eq!(ds.faction_of(kharn).unwrap().id.as_str(), "world-eaters"); ``` -------------------------------- ### Accessing Embedded Dataset and Unit Abilities Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/data/index.html Demonstrates initializing the embedded dataset and iterating through a unit's abilities and their associated phases. ```rust use wh40kdc::Dataset; let ds = Dataset::embedded(); let kharn = ds.find_unit("Kharn").unwrap(); for ability in ds.abilities_of(kharn) { let phases = ds.phases_of(ability); println!("{} acts in {:?}", ability.ability_id.as_str(), phases); } ``` -------------------------------- ### Find matches with indices Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html Returns an iterator over disjoint matches and their starting indices. ```rust let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect(); assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); let v: Vec<_> = "1abcabc2".match_indices("abc").collect(); assert_eq!(v, [(1, "abc"), (4, "abc")]); let v: Vec<_> = "ababa".match_indices("aba").collect(); assert_eq!(v, [(0, "aba")]); // only the first `aba` ``` -------------------------------- ### pub fn windows(&self, size: usize) -> Windows<'_, T> Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.PhaseList.html Returns an iterator over all contiguous windows of length size. ```APIDOC ## pub fn windows(&self, size: usize) -> Windows<'_, T> ### Description Returns an iterator over all contiguous windows of length `size`. The windows overlap. If the slice is shorter than `size`, the iterator returns no values. ### Panics Panics if `size` is zero. ``` -------------------------------- ### Initialize a new PlayerGame Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/scoring/fn.empty_player_game.html Creates a fresh player game instance using the specified scoring approach. ```rust pub fn empty_player_game(approach: ScoringMode) -> PlayerGame ``` -------------------------------- ### DeploymentPatternZonesItemName Definition and Schema Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.DeploymentPatternZonesItemName.html The struct definition and its corresponding JSON schema constraints. ```rust pub struct DeploymentPatternZonesItemName(/* private fields */); ``` ```json { "type": "string", "maxLength": 128, "minLength": 1 } ``` -------------------------------- ### Get last element Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Returns the last element of the slice, or None if it is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&30), v.last()); let w: &[i32] = &[]; assert_eq!(None, w.last()); ``` -------------------------------- ### pub fn describe_ability(a: &Ability) -> String Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/translate/fn.describe_ability.html Generates a full text description for an ability. ```APIDOC ## describe_ability ### Description Generates a full text description for an ability. This function mirrors the `describeAbility` functionality. ### Signature `pub fn describe_ability(a: &Ability) -> String` ### Parameters - **a** (&Ability) - Required - A reference to the Ability object to describe. ### Returns - **String** - The generated text description of the ability. ``` -------------------------------- ### Get first element Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html Returns the first element of the slice, or None if it is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&10), v.first()); let w: &[i32] = &[]; assert_eq!(None, w.first()); ``` -------------------------------- ### Get string capacity with capacity Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html Returns the String's capacity in bytes. ```rust let s = String::with_capacity(10); assert!(s.capacity() >= 10); ``` -------------------------------- ### pub fn trim_start_matches
(&self, pat: P) -> &str Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.DetachmentName.html Returns a string slice with all prefixes that match a pattern repeatedly removed. ```APIDOC ## pub fn trim_start_matches
(&self, pat: P) -> &str
### Description
Returns a string slice with all prefixes that match a pattern repeatedly removed. The pattern can be a `&str`, `char`, a slice of `char`s, or a function or closure that determines if a character matches.
```
--------------------------------
### chunks_exact_mut
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.KeywordList.html
Returns an iterator over mutable chunk_size elements of the slice at a time, starting at the beginning.
```APIDOC
## pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T>
### Description
Returns an iterator over `chunk_size` elements of the slice at a time, starting at the beginning of the slice. The chunks are mutable slices and do not overlap.
### Parameters
- **chunk_size** (usize) - Required - The size of each chunk.
### Panics
Panics if `chunk_size` is zero.
```
--------------------------------
### Demonstrate split_inclusive behavior
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.LeaderAttachmentEligibleBodyguardKeywordsItem.html
Shows how split_inclusive retains the matched pattern as the terminator of each substring.
```rust
assert_eq!(d, &["a", "b", "c"]);
```
```rust
let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb."
.split_inclusive('\n').collect();
assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);
```
```rust
let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n"
.split_inclusive('\n').collect();
assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]);
```
--------------------------------
### DeploymentPatternName Definition and Schema
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.DeploymentPatternName.html
The struct definition and its corresponding JSON schema validation rules.
```rust
pub struct DeploymentPatternName(/* private fields */);
```
```json
{
"type": "string",
"maxLength": 128,
"minLength": 1
}
```
--------------------------------
### Get byte slice with as_bytes
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html
Returns a byte slice of the String's contents.
```rust
let s = String::from("hello");
assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
```
--------------------------------
### trim_start(&self) -> &str
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.DeploymentPatternName.html
Returns a string slice with leading whitespace removed.
```APIDOC
## pub fn trim_start(&self) -> &str
### Description
Returns a string slice with leading whitespace removed. Whitespace is defined according to the Unicode Derived Core Property White_Space.
```
--------------------------------
### Check character boundaries
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.AlliedRuleName.html
Verifies if an index corresponds to the start of a UTF-8 code point sequence.
```rust
let s = "Löwe 老虎 Léopard";
assert!(s.is_char_boundary(0));
// start of `老`
assert!(s.is_char_boundary(6));
assert!(s.is_char_boundary(s.len()));
// second byte of `ö`
assert!(!s.is_char_boundary(2));
// third byte of `老`
assert!(!s.is_char_boundary(8));
```
--------------------------------
### Swapping Elements in a Slice
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html
Provides examples for both safe and unchecked element swapping within a slice.
```rust
let mut v = ["a", "b", "c", "d", "e"];
v.swap(2, 4);
assert!(v == ["a", "b", "e", "d", "c"]);
```
```rust
#![feature(slice_swap_unchecked)]
let mut v = ["a", "b", "c", "d"];
// SAFETY: we know that 1 and 3 are both indices of the slice
unsafe { v.swap_unchecked(1, 3) };
assert!(v == ["a", "d", "c", "b"]);
```
--------------------------------
### Identify ranges of matching items using partition_point
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.GameModes.html
Shows how to find the range of indices for matching elements and handle cases where an item is not found.
```rust
let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
let low = s.partition_point(|x| x < &1);
assert_eq!(low, 1);
let high = s.partition_point(|x| x <= &1);
assert_eq!(high, 5);
let r = s.binary_search(&1);
assert!((low..high).contains(&r.unwrap()));
assert!(s[..low].iter().all(|&x| x < 1));
assert!(s[low..high].iter().all(|&x| x == 1));
assert!(s[high..].iter().all(|&x| x > 1));
// For something not found, the "range" of equal items is empty
assert_eq!(s.partition_point(|x| x < &11), 9);
assert_eq!(s.partition_point(|x| x <= &11), 9);
assert_eq!(s.binary_search(&11), Err(9));
```
--------------------------------
### Into Trait Implementation
Source: https://docs.rs/wh40kdc/1.2.0/wh40kdc/struct.UnitCompositionModelsItemName.html
Conversion implementation based on the From trait.
```rust
impl