How to read {:?}

hi. i'm reading the online Rust book and i come across this

let a = [5; 10];
  let mut sum = 0;

  for x in a {
    sum += x;
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

how do you read or search for {:?} or {:#?}

Those are format arguments and parameters (sometimes also called format specifiers)

You can read more about them in the docs for the std::fmt module


thanks for the link. i actually was looking for an explanation like

  (integer == 5) ? (TRUE) : (FALSE);

which is an if-then shortcut. i thought it is an operator.

gotta remember it's a format specifier only.

oh, that's a ternary operator.
Rust doesn't have the C ternary, what you'd use as a ? b : c. but that's because in Rust, everything is an expression, so you can just use an if: if a { b } else { c }.

what the book was referring to with {:?} is a format string which tells the formatter to use the Debug formatting for that type, instead of the Display formatting. If you're confused by this, don't worry about it, you'll get used to it later.

1 Like

{:?} is a format parameter with debug formatting. {:#?} is an extended debug format parameter.

In recent Rust versions {variable_name:?} is also supported.

1 Like

Just try those, and you will catch the main idea - ? prints additional info, for "programmers"..

println!("{sum}"   );
println!("{sum:?}" );
// or the same
println!("{}" , sum  );
println!("{:?}" , sum);
println!("{:#?}", sum);

After that, if you want, you can search for Display and Debug Traits.

You can actually type it right into the search box in the docs!



This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.