Open kangalio opened 3 years ago
Naively splitting by semicolons would break quite quickly, for example when putting semicolons in colons or in strings. A more robust way would be to parse the source code using something like syn. This brings plenty problems though:
This all seems quite clunky and error prone. Does someone have an idea how to do it better?
Problem
Currently, the code provided to the
?eval
command is wrapped like this:This has a small issue; when the code evaluates to a value with a lifetime, the code fails to compile, for example:
That's because
&a
exits the scope we explicitly declared with {}, but its lifetime is bound to something within that scope (a
).This is slightly confusing for bot users and also a bit annoying.
How to fix
You may be able to prevent this issue by wrapping the code differently. The following ?eval input...
...would be translated to the following Rust code...
In other words: the code will be split up by semicolons and only the result of the last line is put into the
println!()
. The rest are top-level statements inside main.I'm not sure if there are edge cases in which this approach would break