https://github.com/mindsbackyard/trim-margin
A utility crate to help with layouting multi-line strings by detecting their margin.
https://github.com/mindsbackyard/trim-margin
margin rust string utils
Last synced: about 2 months ago
JSON representation
A utility crate to help with layouting multi-line strings by detecting their margin.
- Host: GitHub
- URL: https://github.com/mindsbackyard/trim-margin
- Owner: mindsbackyard
- License: apache-2.0
- Created: 2018-01-13T22:31:55.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-14T18:13:06.000Z (over 8 years ago)
- Last Synced: 2025-12-14T03:23:43.820Z (6 months ago)
- Topics: margin, rust, string, utils
- Language: Rust
- Size: 7.81 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# trim-margin: easy layouting of multi-line strings
[](https://travis-ci.org/mindsbackyard/trim-margin)
[](https://docs.rs/trim-margin)
[](https://crates.io/crates/trim-margin)
This crate is intended to ease the use of multi-line strings in Rust.
When embedding strings with multiple lines in Rust all whitespaces, tabs, etc. are preserved even if they are just used for layouting one's code nicely.
```Rust
fn main() {
println!("-----------------------");
let misrepresented_multiline_string = "
This is string
spans over multiple lines,
but its rendering preserves all whitespaces.
Which is not what we usually intend in this case.
";
println!("{}", misrepresented_multiline_string);
println!("-----------------------");
println!("-----------------------");
let correctly_layouted_string = "For displaying
the a multiline strin properly
it would need to be layouted
like this.
Which is not very nice.";
println!("{}", correctly_layouted_string);
println!("-----------------------");
}
```
The `trim-margin` crate supports you with proper layouting.
By introducing a margin in the multi-line string the `trim_margin` method can filter out unwanted whitespaces and blank lines.
```Rust
extern crate trim_margin;
use trim_margin::MarginTrimmable;
fn main() {
let multiline_string_with_margin = "
|This string has a margin
|indicated by the '|' character.
|
|The following method call will remove ...
| * a blank first/last line
| * blanks before the margin prefix
| * the margin prefix itself
".trim_margin().unwrap();
println!("{}", multiline_string_with_margin);
}
```