by sinhpham · 8y ago · 0 comments
let lines = {
let ret = vec![];
// Open file
// Read into ret
ret
};
let processed_data = {
// Open another file
// Use lines
// Construct ret
ret
};
....
You get the best of both worlds this way: scoped, meaningful names like with sub-functions and the continuity of a long one.Everything is an expression is an underrated feature of Rust.
List<String> lines;
{
int whatever;
lines = new ArrayList<>();
// modify lines
}
List<Object> processed_data;
{
int whatever;
processed_data = new ArrayList<>();
// modify processed_data, using lines
} if err := func() error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
// work with f
return nil
}(); err != nil {
return err
}