### Collect Result Iterators Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Examples demonstrating how to collect iterators of Results, showing short-circuiting behavior on error. ```rust let v = vec![1, 2]; let res: Result, &'static str> = v.iter().map(|x: &u32| x.checked_add(1).ok_or("Overflow!") ).collect(); assert_eq!(res, Ok(vec![2, 3])); ``` ```rust let v = vec![1, 2, 0]; let res: Result, &'static str> = v.iter().map(|x: &u32| x.checked_sub(1).ok_or("Underflow!") ).collect(); assert_eq!(res, Err("Underflow!")); ``` ```rust let v = vec![3, 2, 1, 10]; let mut shared = 0; let res: Result, &'static str> = v.iter().map(|x: &u32| { shared += x; x.checked_sub(2).ok_or("Underflow!") }).collect(); assert_eq!(res, Err("Underflow!")); assert_eq!(shared, 6); ``` -------------------------------- ### Base16ct Encoding and Decoding Examples Source: https://docs.rs/base16ct Demonstrates the usage of lower, upper, and mixed case Base16 decoding and encoding functions. Shows how to use mutable buffers for results and different output types like `&str` and `String`. ```rust let lower_hex_str = "abcd1234"; let upper_hex_str = "ABCD1234"; let mixed_hex_str = "abCD1234"; let raw = b"\xab\xcd\x12\x34"; let mut buf = [0u8; 16]; // length of return slice can be different from the input buffer! let res = base16ct::lower::decode(lower_hex_str, &mut buf).unwrap(); assert_eq!(res, raw); let res = base16ct::lower::encode(raw, &mut buf).unwrap(); assert_eq!(res, lower_hex_str.as_bytes()); // you also can use `encode_str` and `encode_string` to get // `&str` and `String` respectively let res: &str = base16ct::lower::encode_str(raw, &mut buf).unwrap(); assert_eq!(res, lower_hex_str); let res = base16ct::upper::decode(upper_hex_str, &mut buf).unwrap(); assert_eq!(res, raw); let res = base16ct::upper::encode(raw, &mut buf).unwrap(); assert_eq!(res, upper_hex_str.as_bytes()); // In cases when you don't know if input contains upper or lower // hex-encoded value, then use functions from the `mixed` module let res = base16ct::mixed::decode(lower_hex_str, &mut buf).unwrap(); assert_eq!(res, raw); let res = base16ct::mixed::decode(upper_hex_str, &mut buf).unwrap(); assert_eq!(res, raw); let res = base16ct::mixed::decode(mixed_hex_str, &mut buf).unwrap(); assert_eq!(res, raw); ``` -------------------------------- ### Unwrapping with Expect for Environment Variables Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Example of using expect to retrieve an environment variable, providing a clear message if the variable is not set. This pattern emphasizes the expected state of the program. ```rust let path = std::env::var("IMPORTANT_PATH") .expect("env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`"); ``` -------------------------------- ### Get Metadata with Fallible Operations Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Demonstrates using `metadata().and_then(|md| md.modified())` to get file modification times, handling potential errors like 'NotFound'. Note the platform-specific behavior of '/' on Windows. ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` -------------------------------- ### Provide Default Value with `unwrap_or` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `unwrap_or` to get the `Ok` value or a provided default if the Result is `Err`. The default value is eagerly evaluated. ```rust let default = 2; let x: Result = Ok(9); assert_eq!(x.unwrap_or(default), 9); let x: Result = Err("error"); assert_eq!(x.unwrap_or(default), default); ``` -------------------------------- ### Compute Default Value with `unwrap_or_else` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `unwrap_or_else` to get the `Ok` value or compute a default value using a closure if the Result is `Err`. This allows for lazy evaluation of the default. ```rust fn count(x: &str) -> usize { x.len() } assert_eq!(Ok(2).unwrap_or_else(count), 2); assert_eq!(Err("foo").unwrap_or_else(count), 3); ``` -------------------------------- ### Unchecked Unwrap of Ok Value Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `unwrap_unchecked` to get the `Ok` value without checking if it's an `Err`. This is unsafe and leads to undefined behavior if called on an `Err`. ```rust let x: Result = Ok(2); assert_eq!(unsafe { x.unwrap_unchecked() }, 2); ``` -------------------------------- ### Unchecked Unwrap of Err Value Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `unwrap_err_unchecked` to get the `Err` value without checking if it's an `Ok`. This is unsafe and leads to undefined behavior if called on an `Ok`. ```rust let x: Result = Err("emergency failure"); assert_eq!(unsafe { x.unwrap_err_unchecked() }, "emergency failure"); ``` -------------------------------- ### Handling File Metadata with `and_then` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Illustrates using `and_then` to chain operations that might fail, such as retrieving file metadata. ```APIDOC ## File Metadata Example with `and_then` ### Description Shows how `and_then` can be used to chain operations that return a `Result`, like accessing file metadata. ### Method `and_then` ### Example Usage ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` ``` -------------------------------- ### Providing an Alternative `Result` with `or` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Explains how `or` returns the `Ok` value of the first `Result` if it's `Ok`, otherwise returns the second `Result`. ```APIDOC ## `or` Method ### Description Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`. Arguments passed to `or` are eagerly evaluated. ### Method `or` ### Parameters - **res** (Result) - The alternative result to return if `self` is `Err`. ### Example Usage ```rust let x: Result = Ok(2); let y: Result = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("early error"); let y: Result = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("not a 2"); let y: Result = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result = Ok(2); let y: Result = Ok(100); assert_eq!(x.or(y), Ok(2)); ``` ``` -------------------------------- ### into_ok() Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the contained Ok value, but never panics. Unlike unwrap, this method is known to never panic on the result types it is implemented for. ```APIDOC ## into_ok() ### Description Returns the contained `Ok` value, but never panics. Unlike `unwrap`, this method is known to never panic on the result types it is implemented for. Therefore, it can be used instead of `unwrap` as a maintainability safeguard that will fail to compile if the error type of the `Result` is later changed to an error that can actually occur. ### Method `into_ok()` ### Request Example ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } let s: String = only_good_news().into_ok(); println!("{s}"); ``` ``` -------------------------------- ### Result Implementations Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Details various implementations for the Result type, including methods for copying, cloning, transposing, flattening, and checking status. ```APIDOC ### impl Result<&T, E> #### pub const fn copied(self) -> Result Maps a `Result<&T, E>` to a `Result` by copying the contents of the `Ok` part. ##### Examples ```rust let val = 12; let x: Result<&i32, i32> = Ok(&val); assert_eq!(x, Ok(&12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` #### pub fn cloned(self) -> Result Maps a `Result<&T, E>` to a `Result` by cloning the contents of the `Ok` part. ##### Examples ```rust let val = 12; let x: Result<&i32, i32> = Ok(&val); assert_eq!(x, Ok(&12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` ``` ```APIDOC ### impl Result<&mut T, E> #### pub const fn copied(self) -> Result Maps a `Result<&mut T, E>` to a `Result` by copying the contents of the `Ok` part. ##### Examples ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` #### pub fn cloned(self) -> Result Maps a `Result<&mut T, E>` to a `Result` by cloning the contents of the `Ok` part. ##### Examples ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` ``` ```APIDOC ### impl Result, E> #### pub const fn transpose(self) -> Option> Transposes a `Result` of an `Option` into an `Option` of a `Result`. `Ok(None)` will be mapped to `None`. `Ok(Some(_))` and `Err(_)` will be mapped to `Some(Ok(_))` and `Some(Err(_))`. ##### Examples ```rust #[derive(Debug, Eq, PartialEq)] struct SomeErr; let x: Result, SomeErr> = Ok(Some(5)); let y: Option> = Some(Ok(5)); assert_eq!(x.transpose(), y); ``` ``` ```APIDOC ### impl Result, E> #### pub const fn flatten(self) -> Result Converts from `Result, E>` to `Result` ##### Examples ```rust let x: Result, u32> = Ok(Ok("hello")); assert_eq!(Ok("hello"), x.flatten()); let x: Result, u32> = Ok(Err(6)); assert_eq!(Err(6), x.flatten()); let x: Result, u32> = Err(6); assert_eq!(Err(6), x.flatten()); ``` Flattening only removes one level of nesting at a time: ```rust let x: Result, u32>, u32> = Ok(Ok(Ok("hello"))); assert_eq!(Ok(Ok("hello")), x.flatten()); assert_eq!(Ok("hello"), x.flatten().flatten()); ``` ``` ```APIDOC ### impl Result #### pub const fn is_ok(&self) -> bool Returns `true` if the result is `Ok`. ##### Examples ```rust let x: Result = Ok(-3); assert_eq!(x.is_ok(), true); let x: Result = Err("Some error message"); assert_eq!(x.is_ok(), false); ``` #### pub fn is_ok_and(self, f: F) -> bool where F: FnOnce(T) -> bool, Returns `true` if the result is `Ok` and the value inside of it matches a predicate. ##### Examples ```rust let x: Result = Ok(2); assert_eq!(x.is_ok_and(|x| x > 1), true); let x: Result = Ok(0); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Err("hey"); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Ok("ownership".to_string()); assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` #### pub const fn is_err(&self) -> bool Returns `true` if the result is `Err`. ##### Examples ```rust let x: Result = Ok(-3); assert_eq!(x.is_err(), false); let x: Result = Err("Some error message"); assert_eq!(x.is_err(), true); ``` #### pub fn is_err_and(self, f: F) -> bool where F: FnOnce(E) -> bool, Returns `true` if the result is `Err` and the value inside of it matches a predicate. ``` -------------------------------- ### Unchecked Unwrap (`unwrap_unchecked`) Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Provides documentation for `unwrap_unchecked`, which returns the `Ok` value without checking if it's an `Err`, emphasizing its safety implications. ```APIDOC ## `unwrap_unchecked` Method ### Description Returns the contained `Ok` value, consuming the `self` value, without checking that the value is not an `Err`. Calling this method on an `Err` is undefined behavior. ### Method `unwrap_unchecked` ### Safety Calling this method on an `Err` is _undefined behavior_. ### Example Usage ```rust let x: Result = Ok(2); assert_eq!(unsafe { x.unwrap_unchecked() }, 2); // let x: Result = Err("emergency failure"); // unsafe { x.unwrap_unchecked() }; // Undefined behavior! ``` ``` -------------------------------- ### Unwrap Result or Default Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the Ok value or the default value for the type if the result is an Err. ```rust let good_year_from_input = "1909"; let bad_year_from_input = "190blarg"; let good_year = good_year_from_input.parse().unwrap_or_default(); let bad_year = bad_year_from_input.parse().unwrap_or_default(); assert_eq!(1909, good_year); assert_eq!(0, bad_year); ``` -------------------------------- ### Chain Result with And Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the provided result if the current result is Ok, otherwise returns the Err. ```rust let x: Result = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); ``` -------------------------------- ### Providing a Default Value with `unwrap_or` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Explains `unwrap_or`, which returns the `Ok` value or a provided default value if the `Result` is `Err`. ```APIDOC ## `unwrap_or` Method ### Description Returns the contained `Ok` value or a provided default. Arguments passed to `unwrap_or` are eagerly evaluated. ### Method `unwrap_or` ### Parameters - **default** (T) - The default value to return if `self` is `Err`. ### Example Usage ```rust let default = 2; let x: Result = Ok(9); assert_eq!(x.unwrap_or(default), 9); let x: Result = Err("error"); assert_eq!(x.unwrap_or(default), default); ``` ``` -------------------------------- ### Convert Result to Option, discarding errors Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use the `ok` method to convert a `Result` into an `Option`. This consumes the `Result` and returns `Some(T)` if it was `Ok`, or `None` if it was `Err`. ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` -------------------------------- ### Convert &Result to Result<&T, &E> Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `as_ref` to convert a reference to a `Result` into a `Result` of references. This allows inspecting the contents of a `Result` without consuming it. ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` -------------------------------- ### Into Ok Value Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the contained Ok value without panicking, useful for infallible results. ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } let s: String = only_good_news().into_ok(); println!("{s}"); ``` -------------------------------- ### Trait Implementations for Result Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Lists and briefly describes trait implementations available for the `Result` type, such as `Clone`, `Debug`, and `FromIterator`. ```APIDOC ## Trait Implementations for `Result` ### `impl Clone for Result` #### `fn clone(&self) -> Result` Returns a duplicate of the value. #### `fn clone_from(&mut self, source: &Result)` Performs copy-assignment from `source`. ### `impl Debug for Result` #### `fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>` Formats the value using the given formatter. ### `impl FromIterator> for Result` #### `fn from_iter>>(iter: I) -> Result` Creates a collection from an iterator of `Result`s. If any item is an `Err`, the first `Err` is returned. Otherwise, all `Ok` values are collected. ``` -------------------------------- ### Encoding and Decoding Functions Source: https://docs.rs/base16ct/1.0.0/base16ct/all.html Overview of the primary encoding and decoding functions available in the base16ct crate. ```APIDOC ## Encoding Functions ### Description Functions to encode data into base16 (hex) strings or buffers. ### Functions - **lower::encode(src: &[u8], dst: &mut [u8])** - Encodes input into lowercase hex. - **lower::encode_str(src: &[u8]) -> &str** - Encodes input into a lowercase hex string slice. - **lower::encode_string(src: &[u8]) -> String** - Encodes input into a lowercase String. - **upper::encode(src: &[u8], dst: &mut [u8])** - Encodes input into uppercase hex. - **upper::encode_str(src: &[u8]) -> &str** - Encodes input into an uppercase hex string slice. - **upper::encode_string(src: &[u8]) -> String** - Encodes input into an uppercase String. ## Decoding Functions ### Description Functions to decode base16 (hex) encoded data. ### Functions - **lower::decode(src: &[u8], dst: &mut [u8]) -> Result<&[u8], Error>** - Decodes lowercase hex into a buffer. - **lower::decode_vec(src: &[u8]) -> Result, Error>** - Decodes lowercase hex into a new Vec. - **mixed::decode(src: &[u8], dst: &mut [u8]) -> Result<&[u8], Error>** - Decodes mixed-case hex into a buffer. - **mixed::decode_vec(src: &[u8]) -> Result, Error>** - Decodes mixed-case hex into a new Vec. - **upper::decode(src: &[u8], dst: &mut [u8]) -> Result<&[u8], Error>** - Decodes uppercase hex into a buffer. - **upper::decode_vec(src: &[u8]) -> Result, Error>** - Decodes uppercase hex into a new Vec. ``` -------------------------------- ### HexDisplay Auto Trait Implementations Source: https://docs.rs/base16ct/1.0.0/base16ct/struct.HexDisplay.html Lists the auto trait implementations for HexDisplay, indicating its thread safety and other fundamental properties. ```APIDOC ### Auto Trait Implementations for HexDisplay * `impl<'a> Freeze for HexDisplay<'a>` * `impl<'a> RefUnwindSafe for HexDisplay<'a>` * `impl<'a> Send for HexDisplay<'a>` * `impl<'a> Sync for HexDisplay<'a>` * `impl<'a> Unpin for HexDisplay<'a>` * `impl<'a> UnwindSafe for HexDisplay<'a>` ``` -------------------------------- ### Provide Alternative Result with `or` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `or` to return the `Ok` value of `self` if it's `Ok`, otherwise return the provided `res` Result. Arguments are eagerly evaluated. ```rust let x: Result = Ok(2); let y: Result = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("early error"); let y: Result = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("not a 2"); let y: Result = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result = Ok(2); let y: Result = Ok(100); assert_eq!(x.or(y), Ok(2)); ``` -------------------------------- ### HexDisplay Blanket Implementations Source: https://docs.rs/base16ct/1.0.0/base16ct/struct.HexDisplay.html Details blanket implementations provided for HexDisplay through generic trait bounds. ```APIDOC ### Blanket Implementations for HexDisplay #### `impl Any for T` where `T: 'static + ?Sized` * `fn type_id(&self) -> TypeId`: Gets the `TypeId` of `self`. #### `impl Borrow for T` where `T: ?Sized` * `fn borrow(&self) -> &T`: Immutably borrows from an owned value. #### `impl BorrowMut for T` where `T: ?Sized` * `fn borrow_mut(&mut self) -> &mut T`: Mutably borrows from an owned value. #### `impl CloneToUninit for T` where `T: Clone` * `unsafe fn clone_to_uninit(&self, dest: *mut u8)`: Performs copy-assignment from `self` to `dest`. #### `impl From for T` * `fn from(t: T) -> T`: Returns the argument unchanged. #### `impl Into for T` where `U: From` * `fn into(self) -> U`: Calls `U::from(self)`. #### `impl ToOwned for T` where `T: Clone` * `type Owned = T` * `fn to_owned(&self) -> T`: Creates owned data from borrowed data, usually by cloning. * `fn clone_into(&self, target: &mut T)`: Uses borrowed data to replace owned data, usually by cloning. #### `impl ToString for T` where `T: Display + ?Sized` * `fn to_string(&self) -> String`: Converts the given value to a `String`. #### `impl TryFrom for T` where `U: Into` * `type Error = Infallible` * `fn try_from(value: U) -> Result>::Error>`: Performs the conversion. #### `impl TryInto for T` where `U: TryFrom` * `type Error = >::Error` * `fn try_into(self) -> Result>::Error>`: Performs the conversion. ``` -------------------------------- ### unwrap() Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the contained Ok value, consuming the self value. Panics if the value is an Err, with a panic message provided by the Err's value. ```APIDOC ## unwrap() ### Description Returns the contained `Ok` value, consuming the `self` value. Panics if the value is an `Err`, with a panic message provided by the `Err`’s value. ### Method `unwrap()` ### Request Example ```rust let x: Result = Ok(2); assert_eq!(x.unwrap(), 2); ``` ### Response Example ```rust let x: Result = Err("emergency failure"); x.unwrap(); // panics with `emergency failure` ``` ``` -------------------------------- ### Unwrapping with Expect and Custom Panic Message Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Retrieves the contained Ok value, panicking with a custom message if the Result is Err. Use with caution as it can abort the program. ```rust let x: Result = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure` ``` -------------------------------- ### HexDisplay Trait Implementations Source: https://docs.rs/base16ct/1.0.0/base16ct/struct.HexDisplay.html Details the various trait implementations for the HexDisplay struct, including Clone, Debug, Display, LowerHex, PartialEq, and UpperHex. ```APIDOC ### Trait Implementations for HexDisplay #### `impl<'a> Clone for HexDisplay<'a>` * `fn clone(&self) -> HexDisplay<'a>`: Returns a duplicate of the value. * `fn clone_from(&mut self, source: &Self)`: Performs copy-assignment from `source`. #### `impl<'a> Debug for HexDisplay<'a>` * `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. #### `impl Display for HexDisplay<'_>` * `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. #### `impl LowerHex for HexDisplay<'_>` * `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. #### `impl<'a> PartialEq for HexDisplay<'a>` * `fn eq(&self, other: &HexDisplay<'a>) -> bool`: Tests for `self` and `other` values to be equal. * `fn ne(&self, other: &Rhs) -> bool`: Tests for `!=`. #### `impl UpperHex for HexDisplay<'_>` * `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. #### `impl<'a> Copy for HexDisplay<'a>` #### `impl<'a> Eq for HexDisplay<'a>` #### `impl<'a> StructuralPartialEq for HexDisplay<'a>` ``` -------------------------------- ### and() Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns res if the result is Ok, otherwise returns the Err value of self. Arguments passed to and are eagerly evaluated. ```APIDOC ## and(self, res: Result) -> Result ### Description Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`. Arguments passed to `and` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `and_then`, which is lazily evaluated. ### Method `and(self, res: Result) -> Result` ### Request Example ```rust let x: Result = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); ``` ``` -------------------------------- ### Convert Result to Iterator Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Demonstrates using into_iter to consume a Result, yielding the contained value if Ok or nothing if Err. ```rust let x: Result = Ok(5); let v: Vec = x.into_iter().collect(); assert_eq!(v, [5]); let x: Result = Err("nothing!"); let v: Vec = x.into_iter().collect(); assert_eq!(v, []); ``` -------------------------------- ### Inspecting Ok Values with inspect Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Shows how to perform a side effect, like printing, on the contained value of a Result if it is Ok, without altering the Result itself. ```rust let x: u8 = "4" .parse::() .inspect(|x| println!("original: {x}")) .map(|x| x.pow(3)) .expect("failed to parse number"); ``` -------------------------------- ### Result::ok Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Converts a Result into an Option, consuming the original Result and discarding any error value. ```APIDOC ## pub fn ok(self) -> Option ### Description Converts from `Result` to `Option`. Consumes `self`, and discards the error, if any. ### Method `self.ok()` ### Request Example ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` ### Response Example ```json { "example": "Some(value)" or "None" } ``` ``` -------------------------------- ### Result::unwrap Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the contained Ok value, consuming the self value, or panics if the result is Err. ```APIDOC ## pub fn unwrap(self) -> T ### Description Returns the contained `Ok` value, consuming the `self` value. Panics if the value is an `Err`. ``` -------------------------------- ### fn hash Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Feeds the Result value into the given Hasher. ```APIDOC ## fn hash<__H>(&self, state: &mut __H) ### Description Feeds this value into the given Hasher. ### Parameters - **state** (&mut __H) - Required - The hasher state. ``` -------------------------------- ### fn cmp Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns an Ordering between two Result values. ```APIDOC ## fn cmp(&self, other: &Result) -> Ordering ### Description This method returns an Ordering between self and other. ### Parameters - **other** (&Result) - Required - The other Result to compare against. ``` -------------------------------- ### Convert &mut Result to Result<&mut T, &mut E> Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `as_mut` to convert a mutable reference to a `Result` into a `Result` of mutable references. This enables in-place modification of the `Result`'s contents. ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` -------------------------------- ### Result::product Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Calculates the product of elements in an iterator of Results. Short-circuits on the first Err. ```APIDOC ## fn product(iter: I) -> Result ### Description Takes each element in the `Iterator`: if it is an `Err`, no further elements are taken, and the `Err` is returned. Should no `Err` occur, the product of all elements is returned. ### Method `product` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust let nums = vec!["5", "10", "1", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert_eq!(total, Ok(100)); let nums = vec!["5", "10", "one", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert!(total.is_err()); ``` ### Response #### Success Response (200) - **product** (T) - The product of all elements in the iterator. #### Response Example ```json { "example": "Ok(100)" } ``` ``` -------------------------------- ### Result Chaining with `and_then` Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Demonstrates chaining fallible operations using `and_then`, where the closure is only executed if the preceding operation is successful (Ok). ```APIDOC ## `and_then` Example ### Description Chains fallible operations. If the `Result` is `Ok`, the closure is applied to the contained value. If it's `Err`, the `Err` is returned. ### Method `and_then` ### Example Usage ```rust fn sq_then_to_string(x: u32) -> Result { x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") } assert_eq!(Ok(2).and_then(sq_then_to_string), Ok("4".to_string())); assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed")); assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number")); ``` ``` -------------------------------- ### Result::map_or_default Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Maps a Result to a U using a function for Ok, or the default value of U for Err. (Nightly-only) ```APIDOC ## pub const fn map_or_default(self, f: F) -> U ### Description Maps a `Result` to a `U` by applying function `f` to the contained value if the result is `Ok`, otherwise if `Err`, returns the default value for the type `U`. 🔬This is a nightly-only experimental API. (`result_option_map_or_default`) ### Method `self.map_or_default(f)` ### Parameters #### Function Parameter `f` - **f** (FnOnce(T) -> U) - The function to apply to the `Ok` value. ### Request Example ```rust #![feature(result_option_map_or_default)] let x: Result<_, &str> = Ok("foo"); let y: Result<&str, _> = Err("bar"); assert_eq!(x.map_or_default(|x| x.len()), 3); assert_eq!(y.map_or_default(|y| y.len()), 0); ``` ### Response Example ```json { "example": "U" } ``` ``` -------------------------------- ### Result::expect Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the contained Ok value or panics with a provided message if the result is Err. ```APIDOC ## pub fn expect(self, msg: &str) -> T ### Description Returns the contained `Ok` value, consuming the `self` value. Panics if the value is an `Err`, with a panic message including the passed message and the content of the `Err`. ### Parameters - **msg** (&str) - Required - The message to include in the panic if the result is Err. ``` -------------------------------- ### fn from_iter Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Takes each element in the Iterator: if it is an Err, no further elements are taken, and the Err is returned. Should no Err occur, a container with the values of each Result is returned. ```APIDOC ## fn from_iter(iter: I) -> Result ### Description Takes each element in the Iterator: if it is an Err, no further elements are taken, and the Err is returned. Should no Err occur, a container with the values of each Result is returned. ### Parameters - **iter** (IntoIterator>) - Required - The iterator of Results to collect. ``` -------------------------------- ### Map Result with fallback functions Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `map_or_else` to apply one of two functions based on whether the `Result` is `Ok` or `Err`. This is useful for unpacking results while handling errors. ```rust let k = 21; let x : Result<_, &str> = Ok("foo"); assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 3); let x : Result<&str, _> = Err("bar"); assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); ``` -------------------------------- ### Unwrap Result Value Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Extracts the Ok value or panics if the result is an Err. ```rust let x: Result = Ok(2); assert_eq!(x.unwrap(), 2); ``` ```rust let x: Result = Err("emergency failure"); x.unwrap(); // panics with `emergency failure` ``` -------------------------------- ### decode Source: https://docs.rs/base16ct/1.0.0/base16ct/mixed/index.html Decodes a mixed Base16 (hex) string into a provided destination buffer. ```APIDOC ## decode ### Description Decode a mixed Base16 (hex) string into the provided destination buffer. ### Method Not applicable (Rust function) ### Endpoint Not applicable (Rust function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response None (modifies destination buffer in-place) #### Response Example None ``` -------------------------------- ### Struct HexDisplay Source: https://docs.rs/base16ct/1.0.0/base16ct/struct.HexDisplay.html Represents binary data encoded as hexadecimal (Base16). ```APIDOC ## Struct HexDisplay ### Summary ```rust pub struct HexDisplay<'a>(pub &'a [u8]); ``` ### Description `core::fmt` presenter for binary data encoded as hexadecimal (Base16). ### Tuple Fields * `0: &'a [u8]` - The byte slice to be displayed as hexadecimal. ``` -------------------------------- ### Transpose Result, E> to Option> Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Transposes a Result containing an Option into an Option containing a Result. Ok(None) becomes None, while Ok(Some(_)) and Err(_) become Some(Ok(_)) and Some(Err(_)) respectively. ```rust #[derive(Debug, Eq, PartialEq)] struct SomeErr; let x: Result, SomeErr> = Ok(Some(5)); let y: Option> = Some(Ok(5)); assert_eq!(x.transpose(), y); ``` -------------------------------- ### unwrap_or_default() Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the contained Ok value or a default value. Consumes the self argument then, if Ok, returns the contained value, otherwise if Err, returns the default value for that type. ```APIDOC ## unwrap_or_default() ### Description Returns the contained `Ok` value or a default value. Consumes the `self` argument then, if `Ok`, returns the contained value, otherwise if `Err`, returns the default value for that type. ### Method `unwrap_or_default()` ### Request Example ```rust let good_year_from_input = "1909"; let bad_year_from_input = "190blarg"; let good_year = good_year_from_input.parse().unwrap_or_default(); let bad_year = bad_year_from_input.parse().unwrap_or_default(); assert_eq!(1909, good_year); assert_eq!(0, bad_year); ``` ``` -------------------------------- ### Map Result to a default value Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `map_or_default` to apply a function to the `Ok` value or return the default value of the target type if the `Result` is `Err`. This API is currently experimental and nightly-only. ```rust #![feature(result_option_map_or_default)] let x: Result<_, &str> = Ok("foo"); let y: Result<&str, _> = Err("bar"); assert_eq!(x.map_or_default(|x| x.len()), 3); assert_eq!(y.map_or_default(|y| y.len()), 0); ``` -------------------------------- ### Encode byte slice to Base16 String Source: https://docs.rs/base16ct/1.0.0/base16ct/lower/fn.encode_string.html Encodes an input byte slice into a String containing lower Base16 (hex). Available only when the `alloc` crate feature is enabled. Panics if the input length is greater than `usize::MAX/2`. ```rust pub fn encode_string(input: &[u8]) -> String ``` -------------------------------- ### Map Ok value in Result Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Use `map` to apply a function to the `Ok` value of a `Result`, returning a new `Result` with the transformed value. `Err` values are passed through unchanged. ```rust let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::().map(|i| i * 2) { Ok(n) => println!(/* */"{n}"), Err(..) /* */=> {} } } ``` -------------------------------- ### and_then() Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Calls op if the result is Ok, otherwise returns the Err value of self. This function can be used for control flow based on Result values. ```APIDOC ## and_then(self, op: F) -> Result where F: FnOnce(T) -> Result ### Description Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`. This function can be used for control flow based on `Result` values. ### Method `and_then(self, op: F) -> Result` ### Parameters #### Request Body - **op** (FnOnce(T) -> Result) - Required - A closure that takes the Ok value and returns a Result. ``` -------------------------------- ### Coercing Ok Value with as_deref Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Converts a Result to Result<&::Target, &E> by applying Deref to the Ok value. Useful for borrowing the inner value. ```rust let x: Result = Ok("hello".to_string()); let y: Result<&str, &u32> = Ok("hello"); assert_eq!(x.as_deref(), y); let x: Result = Err(42); let y: Result<&str, &u32> = Err(&42); assert_eq!(x.as_deref(), y); ``` -------------------------------- ### Iterating Over Ok Value with iter Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Provides an iterator that yields a reference to the contained value if the Result is Ok, otherwise yields nothing. Useful for functional-style iteration. ```rust let x: Result = Ok(7); assert_eq!(x.iter().next(), Some(&7)); let x: Result = Err("nothing!"); assert_eq!(x.iter().next(), None); ``` -------------------------------- ### encode_str Source: https://docs.rs/base16ct/1.0.0/base16ct/lower/fn.encode_str.html Encodes a byte slice into a lower Base16 (hex) string. ```APIDOC ## encode_str ### Description Encode input byte slice into a `&str` containing lower Base16 (hex). ### Method N/A (This is a function signature, not an HTTP endpoint) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) - **Result** (`Result<&'a str, Error>`) - A Result containing a string slice of the Base16 encoded data or an Error. #### Response Example N/A ``` -------------------------------- ### Expect Error from Result Source: https://docs.rs/base16ct/1.0.0/base16ct/type.Result.html Returns the Err value or panics if the result is an Ok. ```rust let x: Result = Ok(10); x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10` ```