Expand description
§inline-fn
Inline the content of a function without duplicating code. Useful when duplicating code is desired but you are too embarrassed to do it manually.
§Usage
You have a function that you want to inline
ⓘ
// src/lib.rs
fn world() -> String {
"world".to_string()
}
Use the inline-fn
crate to inline the content of a function without duplicating code.
ⓘ
// Option 1: Specify the path to the file containing the function
// relative to the workspace root.
let greetings = format!("Hello, {}", inline_fn!(world, "src/lib.rs"));
// Option 2: Recursively search all files in the workspace root.
// This has an impact on performance.
let greetings = format!("Hello, {}", inline_fn!(world));
Option 2 has an impact on performance until rust-lang#54725 is implemented
§Why
It’s particularly useful for tests where one test is built on top of another.
ⓘ
#[test]
fn payment_works() {
let payment = payment(100);
assert_eq!(payment.value, 100);
// (very long function body)
}
#[test]
fn refund_works() {
inline_fn!(payment_works, "src/lib.rs"); // expands to the content of the previous fn
let refund = refund(payment.id);
assert_eq!(refund.value, payment.value);
}
Macros§
- inline_
fn - Inline a function from a file. Expands to the function body fetched from the specified file.