How to indent closure body


#1

For example

items.iter().filter(|item| {
})
            .map(|item| {
})
            .collect::<Vec<_>>()

#2

Like this?

items.iter()
    .filter(|item| {
        // some code 1
    })
    .map(|item| {
        // some code 2
    })
    .collect::<Vec<_>>();

#3

What if instead of iter I have big variable with some indentation

{
    {
        object.iter()
              .filter(|item| {
                  // Some code that violates 100 column rule
              })
    }
}

#4

Indent however you want such that it makes the code readable. As for the “100 column” thing, I just completely ignore that.


#5

The problem is I don’t know how to indent to make it readable.


#6

Could you please provide a more detailed example then?
Because with your current constraints I can only say: flatten wherever possible.


#7
fn get_directories_in_directory(directory: &Path) -> Result<Vec<String>, String> {
    match read_dir(directory) {
        Ok(result) => {
            let directories = result.filter(|entry| {
                if let Ok(ref entry) = *entry {
                    metadata(entry.path()).unwrap().is_dir()
                } else {
                    false
                }
                                    })
                                    .map(|entry| {
                let path = entry.unwrap().path();
                let path = helper::relative_path_from(&path, directory);
                path.unwrap().to_str().unwrap().to_string()
                                    })
                                    .collect();
            Ok(directories)
        }
        Err(error) => match error.kind() {
            ErrorKind::NotFound => {
                let directory = directory.to_str().unwrap();
                let error = format!("Directory \"{}\" is not exist", directory);
                Err(error)
            }
            _ => Err(error.description().to_string())
        }
    }
}

#8

I’d probably write that like this:

fn get_directories_in_directory(directory: &Path) -> Result<Vec<String>, String> {
    match read_dir(directory) {
        Ok(result) => {
            let directories = result.filter(|entry| {
                if let Ok(ref entry) = *entry {
                    metadata(entry.path()).unwrap().is_dir()
                } else { false }
            }).map(|entry| {
                let path = entry.unwrap().path();
                let path = helper::relative_path_from(&path, directory);

                path.unwrap()
                    .to_str()
                    .unwrap()
                    .to_string()
            }).collect();

            Ok(directories)
        },
        Err(error) => match error.kind() {
            ErrorKind::NotFound => {
                let directory = directory.to_str().unwrap();
                let error = format!("Directory \"{}\" is not exist", directory);
                Err(error)
            }
            _ => Err(error.description().to_string())
        },
    }
}

Opinions will vary on formatting style but if you like parts of it, use them.