rust-macros

Creating a Custom Colored dbg! Macro In Rust

隐身守侯 提交于 2021-02-19 03:28:52
问题 I'd like to create a custom macro similar to the standard dbg! macro, but with the option to use colors via the colored crate. dbg! usually prints something with the format of [path_to_file:line_number] "symbol name" = "symbol value" //[src/gallery/image_slot.rs:231] "my_integer_value_of_12" = "12" How do I access the path/line number [path_to_file:line_number] so I can print it? How do I access the symbol name of a variable? (i.e. print my_var given my_var = 12 ) 回答1: Use the file!, line!,

How to initialize a struct with a series of arguments

你。 提交于 2021-02-08 04:09:42
问题 In many languages, a common constructor idiom is to initialize values of an object using syntax like this pseudocode: constructor Foo(args...) { for arg { object.arg = arg } } Rust at first seems to be no exception. Many impl for a struct include a constructor named new to zip an ordered series of arguments onto the fields of the struct: struct Circle { x: i32, y: i32, radius: i32, } impl Circle { fn new(x: i32, y: i32, radius: i32) -> Circle { Circle { x: x, y: y, radius: radius } } } Doing

Rust println! problem - weird behavior inside the println macro [duplicate]

若如初见. 提交于 2021-01-29 17:50:15
问题 This question already has answers here : Why does my string not match when reading user input from stdin? (3 answers) Closed 1 year ago . I am currently working on a simple "user input" program. The user can enter a number, which I get with std::io::stdin().read_line(&mut let_name_here).ok().expect("Error"); . After getting the user input I want to print it to the console for a review. I have noticed strange behavior within the println! macro. The following code println!("Your input: {}", let

How do I use a macro across module files?

谁说我不能喝 提交于 2021-01-29 08:37:40
问题 I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module. // macros.rs #[macro_export] // or not? is ineffectual for this, afaik macro_rules! my_macro(...) // something.rs use macros; // use macros::my_macro; <-- unresolved import (for obvious reasons) my_macro!() // <-- how? I currently hit the compiler error " macro undefined: 'my_macro' "... which makes sense; the macro system runs

Why can I not access a variable declared in a macro unless I pass in the name of the variable?

*爱你&永不变心* 提交于 2020-12-29 09:49:26
问题 I have this macro: macro_rules! set_vars { ( $($x:ident),* ) => { let outer = 42; $( let $x = outer; )* } } Which expands this invocation: set_vars!(x, y, z); into what I expect (from --pretty=expanded ): let outer = 42; let x = outer; let y = outer; let z = outer; In the subsequent code I can print x , y , and z just fine, but outer seems to be undefined: error[E0425]: cannot find value `outer` in this scope --> src/main.rs:11:5 | 11 | outer; | ^^^^^ not found in this scope I can access the

Issuing a warning at compile time?

心不动则不痛 提交于 2020-06-22 21:26:21
问题 I want to issue a warning at compile time, perhaps from a macro. It should not be silenceable by cap_lints . My current use case is feature deprecation, but there's other possible uses for this. 回答1: This currently isn't possible in stable Rust. However, there is an unstable feature, procedural macro diagnostics, which provides this functionality for procedural macros, via the Diagnostic API. To emit a compiler warning from inside a procedural macro, you would use it like this: #![feature

Issuing a warning at compile time?

前提是你 提交于 2020-06-22 21:24:09
问题 I want to issue a warning at compile time, perhaps from a macro. It should not be silenceable by cap_lints . My current use case is feature deprecation, but there's other possible uses for this. 回答1: This currently isn't possible in stable Rust. However, there is an unstable feature, procedural macro diagnostics, which provides this functionality for procedural macros, via the Diagnostic API. To emit a compiler warning from inside a procedural macro, you would use it like this: #![feature

Can we get the source code location of the caller in a procedural macro attribute?

可紊 提交于 2020-04-07 05:19:45
问题 I have requirement to get the source location of the caller of every method. I am trying to create a proc_macro_attribute to capture the location and print it. #[proc_macro_attribute] pub fn get_location(attr: TokenStream, item: TokenStream) -> TokenStream { // Get and print file!(), line!() of source // Should print line no. 11 item } #[get_location] fn add(x: u32, y: u32) -> u32 { x + y } fn main() { add(1, 5); // Line No. 11 } 回答1: Ready to use solutions are available (see @timotree 's

Can we get the source code location of the caller in a procedural macro attribute?

拜拜、爱过 提交于 2020-04-07 05:18:18
问题 I have requirement to get the source location of the caller of every method. I am trying to create a proc_macro_attribute to capture the location and print it. #[proc_macro_attribute] pub fn get_location(attr: TokenStream, item: TokenStream) -> TokenStream { // Get and print file!(), line!() of source // Should print line no. 11 item } #[get_location] fn add(x: u32, y: u32) -> u32 { x + y } fn main() { add(1, 5); // Line No. 11 } 回答1: Ready to use solutions are available (see @timotree 's

Convert string into TokenStream

穿精又带淫゛_ 提交于 2020-04-05 06:54:06
问题 Given a string ( str ), how can one convert that into a TokenStream in Rust? I've tried using the quote! macro. let str = "4"; let tokens = quote! { let num = #str; }; // #str is a str not i32 The goal here is to generate tokens for some unknown string of code. let thing = "4"; let tokens = quote! { let thing = #thing }; // i32 or let thing = ""4""; let tokens = quote! { let thing = #thing }; // str 回答1: how can one convert [a string] into a TokenStream Rust has a common trait for converting