cost

Function cost 

Source
pub fn cost<C: Cost>(c: C, d: Doc<C>) -> Doc<C>
Expand description

Introduces an extra cost to a document.

This can be used to steer the printer away from making certain choices while still including them as an option. The cost c will be added to whatever cost would normally be assigned to the document.

ยงExample

Normally, when everything fits in the page width, the printer will prefer to print things flatter, since it adds a cost for each newline added to the layout.

let doc = text("hello world") | (text("hello") & hard_nl() & text("world"));
assert_eq!(doc.to_string(), "hello world");

We can make the flatter option have a greater cost, which will cause the printer to choose the taller layout instead.

let doc = cost(DefaultCost(0, 2), text("hello world")) |
          (text("hello") & hard_nl() & text("world"));

assert_eq!(doc.to_string(),
r"hello
world");