Thanks to visit codestin.com
Credit goes to lib.rs

#string-formatting #serialization #general-purpose

format_tools

Collection of mechanisms for formatting and serialization into string

6 releases (breaking)

0.6.0 Aug 12, 2025
0.5.0 Apr 22, 2025
0.4.0 Apr 13, 2025
0.3.0 Apr 5, 2025
0.1.0 Jun 8, 2024

#2983 in Algorithms

Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App

318 downloads per month
Used in 2 crates

MIT license

555KB
5.5K SLoC

Module :: format_tools

experimental rust-status docs.rs Open in Gitpod discord

Collection of mechanisms for formatting and serialization into string.

Basic use-case

Using the to_string_with_fallback macro to convert values to strings with a primary and fallback formatting method.

fn main()
{
  #[ cfg( feature = "enabled" ) ]
  {

    // Import necessary traits and the macro from the `format_tools` crate.
    use core::fmt;
    use format_tools::
    {
      WithDebug,
      WithDisplay,
      to_string_with_fallback,
    };

    // Define a struct that implements both Debug and Display traits.
    struct Both;

    // Implement the Debug trait for the Both struct.
    impl fmt::Debug for Both
    {
      fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
      {
        write!( f, "This is debug" )
      }
    }

    // Implement the Display trait for the Both struct.
    impl fmt::Display for Both
    {
      fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
      {
        write!( f, "This is display" )
      }
    }

    // Define a struct that implements only the Debug trait.
    struct OnlyDebug;

    // Implement the Debug trait for the OnlyDebug struct.
    impl fmt::Debug for OnlyDebug
    {
      fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
      {
        write!( f, "This is debug" )
      }
    }

    // Example usage: Using Both which implements both Debug and Display.
    let src = Both;
    // Convert the struct to a string using `to_string_with_fallback` macro.
    // The primary formatting method WithDisplay is used.
    let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
    let exp = "This is display".to_string();
    // Assert that the result matches the expected value.
    assert_eq!( got, exp );

    // Example usage: Using OnlyDebug which implements only Debug.
    let src = OnlyDebug;
    // Convert the struct to a string using `to_string_with_fallback` macro.
    // The primary formatting method WithDisplay is not available, so the fallback WithDebug is used.
    let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
    let exp = "This is debug".to_string();
    // Assert that the result matches the expected value.
    assert_eq!( got, exp );
    
  }
}

To add to your project

cargo add format_tools

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/foramt_tools_trivial
cargo run

Dependencies

~2.5MB
~43K SLoC