5 ms·
Aside: one Zig (syntax) feature that I really missed in Rust is shown in the second code block, namely prefixed multi-line string literals à la: const text
by Rendello 18d ago
Aside: one Zig (syntax) feature that I really missed in Rust is shown in the second code block, namely prefixed multi-line string literals à la:
const text =
\\This is a long comment
\\But I can split it among lines arbitrarily
\\And keep my indentation.
;
I've started using the Rust macro library `docstr` [1], which does the same thing:
const TEXT: &'static str = docstr!(
/// Now I can do it in Rust, too.
/// I prefer this style a lot of the time
/// for long texts.
);
It even works with macros (example from the docs):
let greeting: String = docstr!(format!
/// Hello, my name is {name}.
/// I am {} years old!
age
);
1. https://docs.rs/docstr/latest/docstr/ https://docs.rs/docstr/latest/docstr/
- Gibbon1 17d agoIn C you can do that. printf ("Things:\n" " thing1=%u\n" " thing2=%u\n" " thing3=%u\n", thing1, thing2, thing3);
- metaltyphoon 17d agoC# solves this so elegantly string text = """ This is a long comment But I can split it And keep my indentation. """;
- Rendello 17d agoI believe the prefixed literals are a direct response to this style of multiline string. The style you showed is pretty common in a programming languages, but language implementers are faces with a tradeoff: - Keep the initial indentation for each line in the string literal; or - Track the indentation level and attempt to remove the whitespace for each line. For a lot of strings, extra whitespace doesn't matter (eg. SQL), but when you don't want it, you end up removing the indentation in the string literal, and having a string like this: fn f() { text = "This is a decent way to format strings, but surely it could be a little nicer indentation-wise, right?"; print(text); } The prefixed lines have the disadvantage of being a pain to use if your editor doesn't have nice multi-line editing like Vim or Sublime. But I think it's a nice option when it's available.