Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Rust already does that. Just that sprintf("Hello %s", username) is written format!("Hello {}", username), which made it easy to now extend it to allow format!("Hello {username}"). Being a late comer made it easy to plan ahead for the future.

But really I didn't really mention it because printf syntax seems to me like exactly the "mini script using in-band signaling and its weird syntax" that GP complained about.



And it's important to note that format (and its relatives) is a macro and not a function. That means that whatever the input syntax is for format, whether you write format!("Hello {}", username) or format!("Hello {username}") it will compile as (something like) "Hello ".to_string() + username (as an aside, I really appreciate the fact that Rust macros are syntactically distinct entities so that you can tell at a glance whether you're doing a function call or something potentially strange is happening by way of a macro).

In contrast, the old-school stdio sprintf (and relatives) will interpret the format string at run time and then read a varargs list to do the interpolation which can lead to run-time errors and buffer-overflow vulnerabilities and so forth.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: