6 ms·
Rust for C++ programmers – part 1: Hello world
- acqq 13y agoHow do you format the arguments passed to println? Do you have to make new types just to do that?
- mhaymo 13y agohttp://static.rust-lang.org/doc/master/std/fmt/index.html#formatting-parameters http://static.rust-lang.org/doc/master/std/fmt/index.html#fo... `println!("{:04d}", 1);` prints "0001". Is that what you're asking?
- Someone 13y agoI think that's what he is asking. Full details at http://static.rust-lang.org/doc/0.10/std/fmt/index.html http://static.rust-lang.org/doc/0.10/std/fmt/index.html By the way, the Rust docs look nice, but finding this was harder than it should be. After searching for 'println', I got at http://static.rust-lang.org/doc/0.10/std/index.html?search=Println http://static.rust-lang.org/doc/0.10/std/index.html?search=P..., which led me to http://static.rust-lang.org/doc/0.10/std/macros/macro.println.html http://static.rust-lang.org/doc/0.10/std/macros/macro.printl..., which says as good as nothing. The other search results (http://static.rust-lang.org/doc/0.10/std/io/stdio/fn.println.html http://static.rust-lang.org/doc/0.10/std/io/stdio/fn.println... and http://static.rust-lang.org/doc/0.10/std/io/stdio/fn.println_args.html http://static.rust-lang.org/doc/0.10/std/io/stdio/fn.println...) are similarly uninformative. It would have helped if the text "For more information, see std::fmt and std::io::stdio" on that first page had hyperlinks. Now, I had to search for "std::fmt" and wade through a sea of results to get at that first page.
- steveklabnik 13y agoMacro documentation is a little bit... off at the moment. :/
- acqq 13y agoYes. Now as it's mentioned "This is a programmer specified conversion which uses the `Show` trait, which works a bit like `toString` in Java." Do I get the content of { } passed to my custom Show, in order to implement all the formatting possibilities? Hint: explaining the mechanisms of handling such common tasks we can really learn a lot about the language. I'd be happy if the tutorial presents the solutions to such real and common tasks.
- dcsommer 13y agoI like this article. It's short and easy for the target audience and has enough content to let C++ developers start to understand Rust. It also hints at and leaves unexplained other features in the language, piqueing the curiosity of the reader.
- coldtea 13y agoGod, I hate the single quote character. Perhaps I've been conditioned by decades of languages with balanced quotes (for strings etc), plus some OCD, I can't stand an open '.
- Al-Khwarizmi 13y agoYou're not the only one... it's rather sad and silly but this makes the language quite unpleasant for me as well.
- coldtea 13y agoMaybe they can change it for 1.0? They've already changed all kinds of sigils and special characters they used to use. I wonder what pcwalton thinks about it. (That's one of the things I like about Rust actually, that they really debate what's best, and check alternative implementations and discard stuff if they feel the need, even listening to community criticism (unlike some other recenty popular language designers who get in a "you're doing it wrong" defensive mode)).
- anon1385 13y agoThere was quite a lot of debate about it at the time as far as I recall but the core devs seemed set on it and have since stated that they aren't planing any more syntax changes[1] so I think it's pretty unlikely they will change it now. I think it's an unfortunate choice as well, although mainly because it's not a very visible character. >they really debate what's best, and check alternative implementations and discard stuff if they feel the need, even listening to community criticism (unlike some other recenty popular language designers who get in a "you're doing it wrong" defensive mode) When this kind of debate came up again last year on the mailing list it ended up with the mods threatening to ban people and the list being put on full moderation[2]. Along with rather unfriendly messages from the mods such as: "Normally I would warn about the code of conduct, but tbh, in this case I think [person criticising the syntax] deserved it" and "when you want to shut people down in the future (a rolethat is very much needed given the number of cranks that Rust attracts"[3] It may have come up again since but I unsubscribed from rust-dev at that point. [1] https://mail.mozilla.org/pipermail/rust-dev/2013-November/006467.html https://mail.mozilla.org/pipermail/rust-dev/2013-November/00... [2] https://mail.mozilla.org/pipermail/rust-dev/2013-November/006523.html https://mail.mozilla.org/pipermail/rust-dev/2013-November/00... [3] https://mail.mozilla.org/pipermail/rust-dev/2013-November/006522.html https://mail.mozilla.org/pipermail/rust-dev/2013-November/00...
- leoc 13y ago> You don't need the `return` keyword in Rust, if the last expression in a function body (or any other body, we'll see more of this later) is not finished with a semicolon, then it is the return value. So `foo` will always return "world". The `return` keyword still exists so we can do early returns. You can replace `"world"` with `return "world";` and it will have the same effect. Is this a bit brittle for a language which is expected to have applications in safety/security-critical coding? It's not hard to imagine side-effecting last expressions with discarded return values being turned into (effective) return statements by an omitted semicolon. Maybe the type system or other bits of the language would prevent this?
- the1 13y agoyes. it'll prevent.
- pcwalton 13y agoThe type system generally prevents this, because you get a type error if the function return type is not as it's declared. More generally, it's not good Rust style to ignore return values, and dropping a type that should not be ignored (e.g. an error) causes the compiler to emit a warning.
- sdegutis 13y ago> it's not good Rust style to ignore return values, and dropping a type that should not be ignored If I remember correctly, much of OpenSSL is not written in "good C style" either. Considering that we're talking about "a language which is expected to have applications in safety/security-critical coding", it seems like a legitimate concern.
- pcwalton 13y agoI knew I would get someone calling me out on "it's not good Rust style". This style is enforced by warnings for important types, such as errors. The semicolon thing is repeatedly brought up as a possible source of errors, and I had concerns about it too at one point, but I've never seen it lead to a single one. There are just too many other safeguards. First of all, you get warnings on unused results for important types (for example, errors). Warning on dropping errors covers the hazards for crypto (and I can go into more detail here if wanted). Second, it's extremely unlikely that changing the return type from unit to something else won't cause a type error later down the line. For instance, returning a unit value from a function declared to return non-unit gives you a type error.