Skip to content

Mixing the two types of positional specifiers does not always produce an error #7

@bhaible

Description

@bhaible

The "Limitations" section at https://crates.io/crates/formatx says: "Intermingling the two types of positional specifiers isn't supported."
and "Examples given below will always panic."

But mixing unnumbered and numbered positional specifiers does not produce an error.

How to reproduce:

use formatx::formatx;

fn identity (s: &str) -> &str
{
  return s;
}

fn main ()
{
  println!("{}", format!("{} {2} {} {1} {4} {} {3} {5} {6}", "zero", "one", "two", "three", "four", "five", "six"));
  println!("{}", formatx!(identity("{} {2} {} {1} {4} {} {3} {5} {6}"), "zero", "one", "two", "three", "four", "five", "six", "seven", "eight").unwrap());
}

Running this program produces:

zero two one one four two three five six
zero two seven one four eight three five six

Which means:

  • For the built-in format!, the first {} accesses argument 0, the second {} accesses argument 1, and so on. (Like it's documented in https://doc.rust-lang.org/std/fmt/ .)
  • For formatx!, the first {} accesses the lowest-numbered arguments that is not consumed by the numbered directives, the second {} accesses the second-to-lowest-numbered such argument, and so on.

It would be good if formatx! would either produce an error in such cases (so that everyone knows it's really unsupported) or would use the same numbering scheme as the built-in format!.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions