Crates.io | to-offset |
lib.rs | to-offset |
version | 0.1.0 |
source | src |
created_at | 2024-10-13 21:27:20.820949 |
updated_at | 2024-10-15 21:24:08.481202 |
description | This crate provides developer-friendly methods to manipulate strings with character indices. |
homepage | |
repository | https://github.com/neilg63/to-offset |
max_upload_size | |
id | 1407697 |
size | 7,132 |
This crate introduces two traits to simplify working with a range of integer types or to constrain generic parameters for safe casting to usize
, following these rules:
-2
with an array length of 10 refers to index 8
(the last but one).0
.The to_offset(length: usize)
method is implemented for i32
, i64
, u8
, u32
, u64
, and usize
. Here, length
represents the maximum offset, akin to the end of a slice range.
let sample_array = [1, 2, 3, 4, 5, 6];
// Example with negative offset
let relative_index_1 = -2;
let result_1 = relative_index_1.to_offset(sample_array.len());
// result_1 is 4, the index of the penultimate element
// Example with overflow
let relative_index_2 = 100;
let result_2 = relative_index_2.to_offset(sample_array.len());
// result_2 is 6, the length of the array
// Example with underflow
let relative_index_3 = -100;
let result_3 = relative_index_3.to_offset(sample_array.len());
// result_3 is 0, the first index
The from_offset(offset: T) -> Option<&T>
method works with arrays or vectors, accepting any integer type that implements ToOffset
.
let sample_array = [1, 2, 3, 4, 5, 6];
// Accessing the penultimate element
let penultimate_element = sample_array.from_offset(-2);
// equals Some(&5), reference to the last but one element
// Negative offset beyond bounds
let end_minus_10 = sample_array.from_offset(-10);
// equals Some(1), the first element as there are only 6 and it would otherwise underflow
let start_plus_10 = sample_array.from_offset(10);
// equals Some(6), the last element as there are only 6