Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I switch off rustfmt for a region of code instead of a single item?

Tags:

rust

rustfmt

#[rustfmt::skip] allows you to skip a "block" of code while formatting, but this requires putting skip on each {} instead of Clang-style on/off

Consider this code:

fn add(a : i32, b : i32) -> i32 { a + b }
fn sub(a : i32, b : i32) -> i32 { a - b }

rustfmt will format this to:

fn add(a: i32, b: i32) -> i32 {
    a + b
}
fn sub(a: i32, b: i32) -> i32 {
    a - b
}

One needs two #[rustfmt::skip] attributes instead of a single on/off.

There is a rustfmt option for single-line functions, but this example is for demonstration purposes only. I want to control any possible rustfmt setting in the region.

like image 425
skgbanga Avatar asked Apr 27 '21 18:04

skgbanga


People also ask

What does rustfmt do?

A tool for formatting Rust code according to style guidelines.

What is Rust FMT?

rustfmt is a tool for formatting Rust source code. You can install it on Fedora by running: $ sudo dnf install rustfmt.


1 Answers

You could put the functions you do not want to format in a module, tag the entire module with a #[rustfmt::skip], then pull in the items to the parent module with use.

#[rustfmt::skip]
mod unformatted {
    pub fn add(a : i32, b : i32) -> i32 { a + b }
    pub fn sub(a : i32, b : i32) -> i32 { a - b }
}

use unformatted::*;

fn main() {
    dbg!(add(2, 3));
}
like image 195
justinas Avatar answered Sep 28 '22 03:09

justinas