Expand description
stub!() can be assigned to a variable:
let username = stub!(String);This allows you to specify just type of the variable and continue working on other code, then come back later and specify its value.
§Examples
fn assign() {
// you can assign stub!() to a variable
let username = stub!(String);
println!("Hello {username}")
}
fn return_position() -> String {
// you can use stub!() like todo!() in return position
stub!()
}
fn infer_type() {
// you can let the compiler automatically infer the type
let status = stub!();
if status { println!("Success") }
}
fn explicit_type() {
// you can specify the type explicitly
let status: bool = stub!();
if status { println!("Success") }
}
fn custom_message() {
// you can add a custom message
let status: bool = stub!("Send a request to GitHub");
if status { println!("Success") }
}
fn impl_example() -> impl core::fmt::Display {
// you can use stub!() in return position even with `impl Trait` return type
// note: `impl Trait` must be written as `impl dyn Trait` due to `macro_rules!` limitation
stub!(impl dyn core::fmt::Display)
}
fn iter_example() -> impl Iterator<Item = u32> {
// use stub_iter!() when the return type is an iterator
stub_iter!()
}
#[cfg(feature = "futures")]
fn stream_example() -> impl futures_core::stream::Stream<Item = u32> {
// use stub_stream!() when the return type is a stream
stub_stream!()
}
fn explicit_type_with_message_example() -> u32 {
// you can add
stub!(u32, "Assigned to: {}", "John")
}
fn explicit_type_example() -> u32 {
stub!(u32)
}
fn implicit_type_with_message_example() -> u32 {
stub!("Assigned to: {}", "John")
}
fn implicit_type_example() -> u32 {
stub!()
}§Behavior
When a stub is invoked, it will panic like a todo!() macro.
However, unlike a todo!() macro, it will not make the subsequent parts of your code unreachable.
If a custom message is provided, it will be included in the panic message.
§Notes
stub!()macro is intended for use during development and should be replaced with actual implementations before production use.- When using
impl Traitin return position, you must useimpl dyn Traitin the macro invocation due to a limitation inmacro_rules! stub!(impl dyn Trait)requires theallocfeature.stub_stream!()requires thefuturesfeature.