Normally one can print strings the following way: println!("{:#?}", foo) where the {:#?} syntax will make a pretty-print of the value. But I know it's also possible to inline the variable directly in the string between the curly braces, instead of listing it as a second argument to the macro, like so: println!("{foo}").
My question is - can I combine the pretty-print syntax and inlining the variable in the string?
I found out about the shorthand syntax from clippy's docs, but I couldn't find (or understand) how to combine it with pretty-print (if it's at all possible).
Simply place the variable name before the colon:
fn main() {
let foo = 3;
println!("{foo:#?}");
}
Note:
:#? is pretty-printed Debug output
:? is normal Debug output
no modifier is Display output
Display is for user-facing output
Debug is for output when debugging, also used for panic messages
Related
Working on my first rust app, and it issues a number of commands via std::process::Command. If one of these is incorrect I'd like to see what it is, and to have it look pretty on the command line.
Currently I have code that looks like this (simplified):
let mut command = std::process::Command::new("ls");
command.arg("-la");
println!("{:?}", command)
This is alright, but it encloses everything in quotes when it prints the string. The output looks like: "ls" "-la".
How can I format this so that it doesn't enclose each arg in double quotes, but instead produces a command that is easy to read? Something like: ls -la.
I saw a related issue, but it comes to the same mediocre solution.
Command adds the quotes because, while they're "ugly," they never break a command. On the other hand, not having them can! Parsing whether you need them or not, including all edge cases, can get surprisingly complicated.
If you have a tightly controlled set of use cases, and you are certain that you don't need them, then just remove them:
let mut command = std::process::Command::new("ls");
command.arg("-la");
println!("{}", format!("{:?}", command).replace("\"", ""));
I would like to fix every line in code which has following pattern:
int main() {
with
int main()
{
Same applies for if statements and loops. Simply said - fixing brackets. I have matched them with:
:%s/.*\(.*\).*{/&^?^M{/gc
But I get the following output:
int main() {
{
How do I replace my pattern with it's match (&), but without the last character or specifically without the "{" ?
%s/.*(.\{-})[^{]*\zs{/\r&/
this command works for your example, add flag g or gc if you need them. however you may want to check if the indentation also correct if you apply it on you real source file.
I think there should be special tool for the code style fixing.. you should check it. IMO, Vim/shell script would be the last option for those source codes batch editing.
When working with blocks of code in VIM, I'm able to easily re-indent blocks of code via selecting a region in visual mode (SHIFT+v), then just hit =. This re-tabs lines of code, uses the correct indentation depths, hard-tabs vs spaces, etc.
I have a large set of functions I need to re-factor, and I have several blocks of code with braces on the same line as if/else keywords, ie:
if(something) {
doFunction(something);
} else if(somethingElse) {
doFunction(somethingElse);
} else {
// default stuff to do
}
And I would like to change the brace and spacing style to:
if ( something ) {
doFunction( something);
}
else if ( somethingElse )
{
doFunction( somethingElse );
}
else
{
// default stuff to do
}
The differences include:
Having the opening/closing braces on their own dedicated line
The argument to if, else if, and functions has a space separating the beginning and end of the argument list from the surrounding round brackets.
There is a space between if/else if and the argument brackets, but not for function names and the argument brackets.
Is there a way to set this style as the default in VIM, and to also have re-indentation commands change the style to match the latter of the two I've provided? I've found tools to enforce things like line endings, tabs-vs-spaces, etc, but not style details like those shown above.
Thank you.
The indentation scripts in vim are not constructed for so complex tasks. I would advise you to use the indent command, in particular the following arguments:
-prs, --space-after-parentheses
Put a space after every '(' and before every ')'.
See STATEMENTS.
-sai, --space-after-if
Put a space after each if.
See STATEMENTS.
You should read the command's man page for more details.
Obviously, this command can be used to filter the buffer's content using:
:%!indent
With vim's errorformat syntax, is there any way to use part of the message in filtering results?
As an example, some linker errors don't have anything explicit to distinguish them as an error on the line, other than the error itself:
/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction'
or
/path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction'
Using an errorformat of:
set efm=%f:%l:\ %m
would catch and display both of these correctly, but will falsely match many other cases (any line that starts with "[string]:[number]: ").
Or, explicitly specifying them both:
set efm=
set efm+=%f:%l:\ undefined\ reference\ to\ %m
set efm+=%f:%l:\ multiple\ definition\ of\ %m
removes the false positives, but the 'message' becomes far less useful -- the actual error is no longer included (just whatever is after it).
Is there anything in the syntax I'm missing to deal with this situation?
Ideally I'd like to be able to say something along the lines of:
set efm+=%f:%l:\ %{StartMessage}undefined\ reference\ to\ %*\\S%{EndMessage}
set efm+=%f:%l:\ %{StartMessage}multiple\ definition\ of\ %*\\S%{EndMessage}
... where everything matched between StartMessage and EndMessage is used as the error's message.
The errorformat can also use vim's regular expression syntax (albeit in a rather awkward way) which gives us a solution to the problem. We can use a non-capturing group and a zero-width assertion to require the presence of these signaling phrases without consuming them. This then allows the %m to pick them up. As plain regular expression syntax this zero-width assertion looks like:
\%(undefined reference\|multiple definition\)\#=
But in order to use it in efm we need to replace \ by %\ and % by %% and for use in a :set line we need to escape the backslashes, spaces and vertical bar so we finally have:
:set efm=%f:%l:\ %\\%%(undefined\ reference%\\\|multiple\ definitions%\\)%\\#=%m
With that the error file
/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction'
/path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction'
notafile:123: just some other text
comes out as the following in :copen:
/path/to/foo.cpp|42| undefined reference to 'UnimplementedFunction'
/path/to/foo.cpp|43| multiple definition of 'MultiplyDefinedFunction'
|| notafile:123: just some other text
I've been using sed to rewrite the output in cases like this where I want to get some arbitrary output that's not nessicarily homogenous into the quickfix window.
You could write make.sh that fires off make (or whatever your're using to build) and trims off stuff you're not concerned with:
make | sed '/undefined reference\|multiple definition/!d'
(Deletes lines not containing 'undefined reference' or 'multiple definition')
If that's going to get too unweildly because of the number of error strings you care about, you could do the inverse and just kill stuff you don't care about:
make | sed 's/some garbage\|other useless message//'
then :set makeprg=make.sh in vim
Some programming languages e.g. Pascal and Java have a keyword or standard library function e.g. print to output a value, and a separate name println to output with a newline, but this has the disadvantage of giving the shorter/more obvious name to the behavior you don't usually want.
Other languages e.g. Basic and Python use print for the behavior you do usually want and have a special syntax such as a trailing ; or , to suppress the newline when required. This works, but one could argue that it would be more elegant/transparent to have different names for the two behaviors.
Are there - or have there ever been - any languages where output of a value with newline is spelled print, and there is a separate name for output without newline?
Tcl's puts prints the given string and the trailing newline by default, unless it's passed the -nonewline command-line option.