rust-lang / discord-mods-bot

discord moderation bot
MIT License
72 stars 30 forks source link

Add a way to run code that requires neither an explicit `main` nor the printing of a final expression #92

Open Kimundi opened 3 years ago

Kimundi commented 3 years ago

While giving support on the discord channel, I've noticed over time that there are some recurring scenarios where you might want to show how some code runs via the bot, but neither ?play or ?eval are the ideal solutions.

For example, lets say I want to show the output of this code:

println!("{}", std::mem::size_of<u16>());
println!("{}", std::mem::size_of<u128>());

Currently I have two options:

The ?play variant has the desired compact output, but takes more effort to write up due to needing to wrap the code in a main. Depending on the target audience of such an demonstration (ie, very early beginners), it might also be confusing that a function definition got involved.

The ?eval variant is easier to type and more compact, which can make it clearer what is being demonstrated, but it has the disadvantage that it prints the final () if its actually just used for side-effects.

Ideally there would be a simple way to do a mixture of both: Automatically wrap the code in a block and run it, but without unconditionally printing an expression:

I can imagine multiple ways to expose this functionality:

khionu commented 3 years ago

I think the best way for this would be flags for whether to print the stdout and whether to print the overall expression. If we wanted to go the extra mile, a command that explains what happens at each statement may be possible, but it would involve more than syntactic appraisal.