▲ 2 ▼ When your language doesn't allow arbitrary expressions in format strings (programming.dev) submitted 3 years ago by Walnut356@programming.dev to c/programming_horror@programming.dev 3 comments fedilink hide all child comments
[–] nous@programming.dev 1 point 3 years ago (1 child) You can give it variables: let foo = 4; let bar = "bar"; format!("{foo}{bar}"); If you have a format string that long with arbatary expressions putting them all raw in the format string is not going to improve much. Better to give them actual names first. permalink fedilink source hideshow 2 child comments replies: [–] Walnut356@programming.dev [S] 0 points 3 years ago (1 child) in this case it's about 80% function calls. They're convenience functions for assembly instructions, so they're of the form: load(Reg::D, "A"), load_const(5), which is more useful than variables would be. I guess i could use .join or a crate like concat_string? Either way i sorely miss arbitrary expression format strings from python =( permalink fedilink source parent hideshow 2 child comments replies: [–] sukhmel@programming.dev 1 point 3 years ago As far as I remember, the point for not allowing arbitrary expressions is that it makes what looks like formatting an arbitrary complex operation and that it doesn't improve readability that much. Although sometimes I miss being able to refer to fields in format, for function calls and especially this many, I agree with an advice to put strings in a vector and joining them. Plus, there is a limit to how many arguments format macro can accept, iirc permalink fedilink source parent
[–] Walnut356@programming.dev [S] 0 points 3 years ago (1 child) in this case it's about 80% function calls. They're convenience functions for assembly instructions, so they're of the form: load(Reg::D, "A"), load_const(5), which is more useful than variables would be. I guess i could use .join or a crate like concat_string? Either way i sorely miss arbitrary expression format strings from python =( permalink fedilink source parent hideshow 2 child comments replies: [–] sukhmel@programming.dev 1 point 3 years ago As far as I remember, the point for not allowing arbitrary expressions is that it makes what looks like formatting an arbitrary complex operation and that it doesn't improve readability that much. Although sometimes I miss being able to refer to fields in format, for function calls and especially this many, I agree with an advice to put strings in a vector and joining them. Plus, there is a limit to how many arguments format macro can accept, iirc permalink fedilink source parent
[–] sukhmel@programming.dev 1 point 3 years ago As far as I remember, the point for not allowing arbitrary expressions is that it makes what looks like formatting an arbitrary complex operation and that it doesn't improve readability that much. Although sometimes I miss being able to refer to fields in format, for function calls and especially this many, I agree with an advice to put strings in a vector and joining them. Plus, there is a limit to how many arguments format macro can accept, iirc permalink fedilink source parent
all 3 comments