3 ms·
What's with the exclamation mark at the end of function names? e.g. println!("foo")
by Twirrim 11y ago
What's with the exclamation mark at the end of function names? e.g. println!("foo")
- deleted 11y ago[deleted]
- cfallin 11y agoThat signifies a macro. It's meant to visually distinguish macros from ordinary function calls because macros may have arbitrary semantics (they can expand into any code at all). The stdlib defines `println` as a macro because it allows for a neat compile-time parsing of format strings. (See [1] and the linked source if curious.) [1] https://doc.rust-lang.org/std/macro.println!.html https://doc.rust-lang.org/std/macro.println!.html
- Twirrim 11y agoThanks, that makes a lot of sense as a design decision, should be a big step forwards for security.
- Grue3 11y agoSounds like what Rust needs is compile macros [1]. That way, printf can still be a function, but it would be optimized at compile time if the formatting string is constant. [1] https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node101.html https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node101.html
- deleted 11y ago[deleted]
- akjj 11y agoCompile macros wouldn't be sufficient for Rust's println! because the type signature (even the number of arguments) depends on the format string. The format string has to be constant and has to be interpreted at compile time so that the variables being printed can be type-checked.