How to use macro for replacing closing curly brace?

Hi all,
I am new to Rust. Please see this C++ code.
"""----------------------------------------------------------------

#define end }; 
#define log(x)  cout << x << endl
#define wait std::cin.get() 

int main() {
     log( "Trying to avoid visual clutter caused by closing curly brace") ;     
     string myStr = "Now, code looks cleaner..." ;
     log(myStr) ;  
     wait ;    
end 
```cpp
"""----------------------------------------------------------------
How can i do this with Rust macros ?
1 Like

You can use

```
Your code here
```

To pretty print your code

1 Like

You want to replace the "visual clutter" of "}" with the bigger, uglier visual clutter of "end"?

That boggles my mind.

2 Likes

Thanks :slight_smile:

"End " is more readable

For C++ formatting use, this forum defaults to Rust syntax highlighting

```cpp
// your code here 
```
1 Like

Oh, okay. Thanks :slight_smile:

1 Like

You can't do this with Rust macros. You can create the log and wait macros, and but not the emd macro. Rust macros are not fancy text replacement.

3 Likes

Well, i can make this more elegant with

#define end_main };
#define end_if } ;

Now, its more readable.

So sad. Then this is the only option for me.

fn main() {
    println!("Hello, world!");

}//end_main

Not sad at all. If people cannot do such things then I won't have to suffer reading code where people do it. I cannot agree that "end_main" and "end_if" are more readable than a simple "}". It's longer, it's uglier, it's redundant.

I think that if you are having difficulty telling which "}" belongs to which "fn", "if" etc then it is a sure sign that your functions are getting too long and complicated and it's time to get things organized properly.

5 Likes

To answer this more seriously — you can't. Rust syntax requires all parenthesis and braces to be balanced at all times. Macros are not textual find'n'replace. They work on the syntax tree, meaning they can only insert full working expressions and statements.

7 Likes

You could run the C preprocessor on your program first if you are okay with a cumbersome build system.

3 Likes

I prefer words over signs. Thats why i love this style. I know most programming languages uses signs over words, for example, && instead of and || instead of or. Well, this is my personal opinion.There is no issues with my functions.

Thanks for the reply. :slight_smile:

No, I am happy with the current rust style. I would like to change it only if i can do it as easy as c++. Otherwise, i am not interested to make any changes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.