You can use the
cfg_attr(a, b) attribute:
#[derive(Debug, Clone)] #[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct MyStruct;
It’s described in the Rust reference about “conditional compilation”:
#[cfg_attr(a, b)] item
Will be the same as
ais set by
- How do I create a global, mutable singleton?
- What is the difference between iter and into_iter?
- How do I synchronously return a value calculated in an asynchronous Future in stable Rust?
- Why does linking lifetimes matter only with mutable references?
- How do I stop iteration and return an error when Iterator::map returns a Result::Err?
- Returning a closure from a function
- Deriving a trait results in unexpected compiler error, but the manual implementation works
- When does a closure implement Fn, FnMut and FnOnce?
- Does println! borrow or own the variable?
- Passing mutable self reference to method of owned object
- What makes something a “trait object”?
- Proper way to return a new string in Rust
- Is it possible to use `impl Trait` as a function’s return type in a trait definition?
- How to create a static string at compile time
- println! error: expected a literal / format argument must be a string literal
- Initialize a large, fixed-size array with non-Copy types
- When is it useful to define multiple lifetimes in a struct?
- How to initialize struct fields which reference each other
- How to take ownership of T from Arc?
- How does the mechanism behind the creation of boxed traits work?
- Double mutable borrow error in a loop happens even with NLL on
- Borrow two mutable values from the same HashMap
- What is the syntax: `instance.method::()`?
- Weird behaviour when using read_line in a loop
- Is there another option to share an Arc in multiple closures besides cloning it before each closure?
- How do I call a function through a member variable?
- Is it possible to declare the type of the variable in Rust for loops?
- How can I downcast from Box to a trait object type?
- Why do I get “type annotations needed” when using Iterator::collect?
- Lifetime of variables passed to a new thread
- What’s the Rust idiom to define a field pointing to a C opaque pointer?
- How do you use parent module imports in Rust?
- How do I implement a trait with a generic method?
- How can I implement Rust’s Copy trait?
- How to match on data type in Rust?
- Why can’t I return an &str value generated from a String?
- How to implement `serde::Serialize` for a boxed trait object?
- How does Rust pattern matching determine if the bound variable will be a reference or a value?
- How can a Rust program access metadata from its Cargo package?
- How to print structs and arrays?
- How to generate statically linked executables?
- How to concatenate static strings in Rust
- Can array lengths be inferred in Rust?
- Why does Rust allow mutation through a reference field using an immutable binding?
- How can I conditionally provide a default reference without performing unnecessary computation when it isn’t used?
- HashMap borrow issue when trying to implement find or insert
- How to switch between Rust toolchains?
- What is the prelude?
- Lazy sequence generation in Rust
- How can I download Rust API docs?