default_params 1.0.0

Default parameters can be useful
Documentation
  • Coverage
  • 100%
    43 out of 43 items documented29 out of 43 items with examples
  • Size
  • Source code size: 16.92 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.22 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ministry-prog

Default Parameters

Do you want default function arguments or method parameters inside Rust?

Well, too bad! As this feature is currently not supported in Rust we can only emulate this functionality through macros and even more macros.

Formalisation

Default parameters could be easily formalised as

struct DefaultParam<T, const VAL: T>(T);

Reality

This approach however falls apart in a spectacular fashion as Rust doesn't support types dependent on other types.

Sadly this leaves us no choice. We can currently only make default arguments for integer, bool and char types.

How does this implementation work?

  • All supported types have a wrapper around them (i32 -> Di32, bool -> Dbool ...)
  • Empty default arguments can be created via
let def1=Di32::<23>::new(); // Its value will be 23
  • Default arguments with values can be created via
let def2=Di32::<5>::from(53); // Its value will be 53
  • The value of default arguments can be unwrapped via
assert_eq!(def1.unwrap(),23);
assert_eq!(def2.unwrap(),53);