Rustfmt long slice literals

I am struggling with rustfmt. I have code which is matching against
long literals. A single match arm might look like this:

                [[_, Term::OWL(VOWL::OnDatatype), Term::Iri(iri)],
                 [_, Term::OWL(VOWL::WithRestrictions), Term::BNode(id)],
                 [_, Term::RDF(VRDF::Type), Term::RDFS(VRDFS::Datatype)]] =>

Rustfmt turns it into this:

                [[_, Term::OWL(VOWL::OnDatatype), Term::Iri(iri)], [_, Term::OWL(VOWL::WithRestrictions), Term::BNode(id)], [_, Term::RDF(VRDF::Type), Term::RDFS(VRDFS::Datatype)]] =>

Which is both unreadable and very, very long (one of my lines ends up
at 216 lines long).

I have tried judicious use of rustfmt::skip, but I have to put this on
the whole match expression; as these match expressions make up most of
the file, this is a bit pointless.

I also tried trailing comments like so:

                [[_, Term::OWL(VOWL::OnDatatype), Term::Iri(iri)],\\
                 [_, Term::OWL(VOWL::WithRestrictions), Term::BNode(id)],\\
                 [_, Term::RDF(VRDF::Type), Term::RDFS(VRDFS::Datatype)]] =>

which I have heard given as an solution. But this fails to compile --
comments are not accepted at this point, which I am rather surprised
about to be honest.

Any thoughts welcome.

1 Like

Just put a rustfmt::skip on it, to be honest. Sometimes it just does something silly.

2 Likes

These are backslashes. Is this just a typo?

Dearie, dearie me. Well, that would explain why it didn't work.

Unfortunately, rustfmt removes empty trailing comments, so it doesn't help. But sticking \\: seems to work nicely.

you mean //:

It's really going from bad to worse, isn't.

In my defense, I normally get the editor to put in comment marks.

Since almost your entire file is made up of these large matches, you could just stick #![rustfmt::skip] on the entire file, and take care to format your code as you write it. (This would also help in learning the rust style so that you don't really need rustfmt anymore)

Whole-file syntax is not supported: https://github.com/rust-lang/rust/issues/54726

1 Like

In the end, I think putting //: in is not a bad solution. I did not really want to skip the entire file (whether it is possible to do or not). Nor did I want to skip the match statements, because as well as the pattern, all the bodies get missed.

The main problem with it is that this is not standardized and I may come back in six months time wondering what all the //:'s are about.

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.