| Crates.io | default_params |
| lib.rs | default_params |
| version | 1.1.0 |
| created_at | 2024-12-19 08:48:04.454644+00 |
| updated_at | 2024-12-21 06:09:08.573672+00 |
| description | Default parameters can be useful |
| homepage | |
| repository | https://codeberg.org/ministry460/default_params |
| max_upload_size | |
| id | 1488932 |
| size | 18,265 |
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.
Default parameters could be easily formalised as
struct DefaultParam<T, const VAL: T>(T);
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.
i32 -> Di32, bool -> Dbool ...)let def1=Di32::<23>::new(); // Its value will be 23
let def2=Di32::<5>::from(53); // Its value will be 53
assert_eq!(def1.unwrap(),23);
assert_eq!(def2.unwrap(),53);