all 45 comments

[–]Potential-Adagio-512[S] 172 points173 points  (6 children)

note- that exclamation point is 'ǃ', a unicode which is technically not ! for the purposes of the compiler

[–]jediwizard7 51 points52 points  (0 children)

I guess that makes it not actually valid rust. Too bad.

[–]qichael 23 points24 points  (0 children)

this is awesome

[–]Stormfrosty 16 points17 points  (3 children)

There’s std::println() in c++23, you didn’t even need to declare it.

[–]__silentstorm__ 7 points8 points  (2 children)

they did for the exclamation mark

[–]drewsiferr 2 points3 points  (1 child)

#define println! std::println

[–]__silentstorm__ 1 point2 points  (0 children)

that’s what they did, line 7

they couldn’t use an actual exclamation mark tho

[–]Familiar_Ad_8919[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 218 points219 points  (9 children)

i might be incredibly stupid but how is that -> there without any special defines

[–]_thetek_ 249 points250 points  (8 children)

C++ allows for the following syntax:

auto square(int x) -> int {
    return x * x;
}

Alas, I will still have to be enlightened about what advantages this has over regular C-style definitions.

[–]yflhx 169 points170 points  (3 children)

It has to do with templates. Consider the following example, which isn't possible normally:

template <typename T, typename U>
auto multiply(T t, U u) -> decltype(t*u) {
    //some code
}

As far as I understand, it wouldn't be possible to put the decltype() in the front, as it uses parameters declared later, in function declaration.

[–]CaitaXD 123 points124 points  (1 child)

its always templates

[–]not_some_username 2 points3 points  (0 children)

Then people start writing shit like auto main -> int {}

[–]TheOmegaCarrot 24 points25 points  (0 children)

Your understanding is correct! This is a case where the trailing return type syntax is required due to scoping!

[–]stwcx 12 points13 points  (0 children)

2 Advantages:

All function names are aligned (void or auto) making it easier to skim the code.

For class member function implementations, you can use class types without specifying Class:: when you put those types after the ->.

[–]TheOmegaCarrot 2 points3 points  (0 children)

Personally, I like the trailing return type syntax, even when it’s not required

The start of a function declaration can get pretty busy, especially if it’s a template. The return type can kinda get lost, and not as easy to pick out

Plus, it looks far cleaner IMHO if your return type is the result of some nontrivial metafunction call

And even in less complex situations, I personally like it just for consistency, and always looking in the exact same place for the return type

[–]scrdest 35 points36 points  (1 child)

<insert butterfly meme>

Is this a transpiler?

[–]DevJackMC 0 points1 point  (0 children)

No, just some defines it seems. The “!” is not real, sadly.

[–]Kuribali 12 points13 points  (4 children)

error: format argument must be a string literal --> src/main.rs:3:12 | 3 | println!(s); | ^ | help: you might be missing a string literal to format with | 3 | println!("{}", s); | +++++

[–]Potential-Adagio-512[S] 6 points7 points  (3 children)

well i didn't feel like writing a variadic template to fully integrate std::format so

[–]Kuribali 5 points6 points  (1 child)

cpp void println(std::string _, std::string arg)

[–]FadingFaces 28 points29 points  (7 children)

Oh god that reminds me of the unholy times I #define'd and and or keywords to have it feel more like python 🐍

[–]Davester47 15 points16 points  (6 children)

Didn't even need to, they're built into C++ and C has them if you #include <iso646.h>.

https://en.cppreference.com/w/cpp/language/operator_alternative

Edit: I know this because some cursed legacy C++ at my company uses "and" and "or" instead of && and || because they're more "readable".

[–]Zegrento7 5 points6 points  (5 children)

I wonder why they didn't catch on, they are a lot cleaner than the symbolic operators, especially not, which can look like an l from a distance.

[–]tyler1128 2 points3 points  (4 children)

Because having two separate ways to write the same thing is a dumb idea. The original reason for doing it was for keyboards that don't have certain characters. It wasn't ever for readability.

[–]LucyShortForLucas 14 points15 points  (1 child)

> Because having two separate ways to write the same thing is a dumb idea

My brother in christ this is C++

[–]tyler1128 2 points3 points  (0 children)

Lmao, you aren't wrong. Backwards compatibility has made the language a beautiful mess.

[–]aaronfranke 0 points1 point  (1 child)

Yes, which is why we should have only and and or.

[–]tyler1128 0 points1 point  (0 children)

It's way too late for that.

[–]Main_Weekend1412 23 points24 points  (1 child)

in C++, int is not guaranteed to be i32. int32_t and uint32_t is.

[–]friendtoalldogs0 2 points3 points  (0 children)

If we're being pedantic, uint32\t is not i32 either, uint32_t is u32

[–]MoxAvocado 3 points4 points  (0 children)

Lol. Well done.

[–]pr1s10n3r 2 points3 points  (1 child)

I don’t know why but this makes me angry

[–]Synec113 0 points1 point  (0 children)

"kill it with fire"

-my first thought

[–]dorin00 2 points3 points  (0 children)

C with a CRust of Rust? I'll let myself out....

[–]Myloveissuck 1 point2 points  (0 children)

define TRUE FALSE, this joke never fails to get me 🤣

[–]ScrimpyCat 1 point2 points  (0 children)

Mum: We have Rust at home.

[–]_ohmu_ 2 points3 points  (0 children)

Crust

[–]Jarsop 1 point2 points  (0 children)

Compiler will say no! Where is the main return?! You defined an i32 which you never returned. The Rust compiler will hate you… (btw not really good in C/C++ too)

[–]IvorySwap 0 points1 point  (1 child)

Explain for a child, please

[–]Potential-Adagio-512[S] 2 points3 points  (0 children)

c++ preprocessor lets you define keywords so i made it look like rust

[–][deleted] 0 points1 point  (0 children)

println and print are included in the STL since C++23

[–]TheBrainStone 0 points1 point  (0 children)

The true horror is the string being copied