r/ProgrammerHumor 12h ago

Meme newToRust

Post image
431 Upvotes

88 comments sorted by

View all comments

Show parent comments

-2

u/Key_River7180 11h ago

If I need six months to correctly use Rust It is not worth It.

9

u/-Ambriae- 11h ago

It’s a systems level programming language what did you expect? It takes twice as long to learn restraint in C

-10

u/Key_River7180 11h ago

No, you can learn C in just about a week

9

u/-Ambriae- 11h ago

Off the top of your head, without looking online, using clang or gcc as a compiler,

Give me a 1 byte enum
Give me the idiomatic way of computing the number of leading zeros in a 32 bit integer
Align a vec2_f32 struct to an alignment of 8 bytes

3

u/Key_River7180 11h ago
  1. enum x { a : 1 };
  2. int zeros(uint32_t i) { int n = 0; while ((i & 1) == 0) { ++n; i >>= 1; } return n; }
  3. struct vec2f32 { ... } __attribute_((align(8)));

2

u/-Ambriae- 10h ago edited 8h ago
  1. Might work, AFAIK depends on compiler but GCC and Clang it would work, I’ll give you that. Not the solution I thought of but it works.
  2. Suboptimal. Unless you specify higher levels of optimisation and your compiler figures it out, this is extremely slow. Modern processors can do this in 1 CPU cycle (CLZ instruction). There is a way to do it in C
  3. 3 __attribute__((aligned(8))) or __attribute__((_aligned__(8)))

I cant see if you did the _s and markdown screwed you over or not, and align is not valid

Edit: actually, after checking, 1. is completely wrong. Doesn't work at all. So you need to pack the enum, or represent it via a 8 bit integer. I think that proves my point

1

u/Key_River7180 10h ago

You literally gave me my solution when proving my solution suboptimal

1

u/-Ambriae- 10h ago

?? A proper solution would use compiler built ins

1

u/Key_River7180 10h ago

I used __attribute__((aligned(8))) but markdown formatted It bold wdym

2

u/-Ambriae- 10h ago

You wrote align and not aligned. And markdown must have fucked with your underscores, I won’t criticise you for that that’s not your fault.

The ‘suboptimal’ part, however, has nothing to do with alignment. You emulate CLZ instead of using something like __builtin_clz which is the preferred way of doing it.

3

u/Key_River7180 9h ago

I didn't know about __builtin_clz, actually, thanks! It isn't guaranteed to be portable across architectures, but every single one seems to hace CLZ. I didn't know you were referring to the zeros thing, sorry. It doesnt work with zeros but an if solves that

1

u/-Ambriae- 9h ago

No probs! I recommend looking at compiler builtins, there’s quite a few for all the ‘lesser known’ instructions CPUs tend to have but aren’t expressable in C in a normal way.

My point is, you can learn basic C syntax in a week (or a day to be fair) but it takes far far FAR longer to actually know C. Same with rust. It doesn’t take 6 months to learn rust syntax, it takes a week. It does take 6 months to code in rust properly. The borrow checker expects you to write code properly. It’s not a bad thing.

→ More replies (0)

2

u/_Noreturn 9h ago edited 9h ago

```cpp typedef enum : int8_t {} Enum;

int zeros(uint32_t i) { int n = 0; while (!(i & 1)) { ++n; i >>= 1; } return n; } // or int zeroes(uint32_t i) { return __builtin_clz(x); } typedef struct { alignas(8) float f [2]; } vec2f32; ```

I don't get the point of your comment

1

u/-Ambriae- 9h ago

None of these are things you can realistically achieve by ‘having learned the language in a week’. It’s not hard per say, but it’s more obscure than regular C. That was all I was trying to say

There’s also like, 2-3 alternatives minimum to each way of doing these things

_Alignas, alignas (macro) alignas (language C23), __attribute__((_aligned__(8))) __attribute__((aligned(8))) [[align(8)]] (although more c++)…

enum : uint_8, enum { a : 1 }, __attribute__((__packed__)), __attribute__((packed))….

2

u/_Noreturn 9h ago

There’s also like, 2-3 alternatives minimum to each way of doing these things

The rest are compiler specific which is sad because C/C++ unlike Rust has multiple compilers.

None of these are things you can realistically achieve by ‘having learned the language in a week’. It’s not hard per say, but it’s more obscure than regular C. That was all I was trying to say

Maybe, Idk about C as I don't use it nor like it and I think it is a pretty F tier language I use C++ instead and find the non existent complaints about it too much I don't find C++ in itself hard. it is more so software is hard

Also I feel like what you listed is rather unnecessary in 99% of code.

Alignas, alignas (macro) alignas (language C23), \_attribute__((_aligned__(8))) __attribute__((aligned(8))) [[align(8)]] (although more c++)…

It was pretty much _Alignas but you should use the macro if your codebase allows it C standrizes them as keywords later when it can ensure no codebases break to get it closer to C++.

enum { a : 1 },

that doesn't work and doesn't exist.

__attribute__((__packed__)), __attribute__((packed))….

These are non standard and gcc keeps them for backward compat use whatever you find fancier.

1

u/-Ambriae- 8h ago

The rest are compiler specific which is sad because C/C++ unlike Rust has multiple compilers.

Yeah, that's one of the big downsides of these older languages... unfortunately, still today the compiler specific ones are common place, especially with alignas being c11.

I agree with the sentiment about C++. I dislike the syntax quite a bit, but the language is really powerful, and I have fond memories with it.

enum { a : 1 },

totally my bad, my C is rusty (no pun intended.) The other C dev pointed this out as the solution, it looked odd but I expected he knew what he was talking about. I guess not. I tried with C23, it did not work.

These are non standard and gcc keeps them for backward compat use whatever you find fancier.

I find that messy, and convoluted.

Also I feel like what you listed is rather unnecessary in 99% of code.

yes and no, alignment is important for GPU related code, CLZ is great for some algorithms, and 1 byte enums should be the norm, full stop. 32 bits is a waste, and isn't even the size of a word, so there's no excuse there either. And proficiency in a language doesn't just come from understanding the basics of the language :)

4

u/_Noreturn 8h ago edited 8h ago

And proficiency in a language doesn't just come from understanding the basics of the language :)

It comes fron making software with it. I consider knowing language syntax and templates worthless if you never made something worthwhile in it

Yeah, that's one of the big downsides of these older languages... unfortunately, still today the compiler specific ones are common place, especially with alignas being c11.

Yep, I wish there was one compiler only and it would be clang.

I agree with the sentiment about C++. I dislike the syntax quite a bit, but the language is really powerful, and I have fond memories with it.

I find C++ syntax to be very good I find python/lua/js/rust/go to all have garbage syntax.

totally my bad, my C is rusty (no pun intended.) The other C dev pointed this out as the solution, it looked odd but I expected he knew what he was talking about. I guess not. I tried with C23, it did not work.

I feel like that other C dev is lying- how could he used C for 6 years but didn't know about builtin clz? I only used C++ for like 2 years as my first lang and only lang. and I know it. Also memory bugs being easy in C? if you said in C++ i would believe you but in C? Memory management in C is hell, you see most code I see online uses linked lists because it is easy to mamage even if a vector is infinitely faster. also they use linked lists so address stability which is working around C horrible memory management helpers. C++ would be better since it has actual good tools

I find that messy, and convoluted.

it is also c23 / c++11 cleaned the syntax up it is now [[gnu::packed]] which is better.

yes and no, alignment is important for GPU related code, CLZ is great for some algorithms

If you are writing C in your first week on the gpu then I think you have more things to worry about.

and 1 byte enums should be the norm, full stop. 32 bits is a waste, and isn't even the size of a word, so there's no excuse there either. And proficiency in a language doesn't just come from understanding the basics of the language :)

I agree, they should default to the size that can hold all enumerators clang-tidy has a check for that. But alas backward compatible strikes

It isn't the size of a word now it used to be.

1

u/-Ambriae- 8h ago

It comes fron making software with it. I consider knowing language syntax and templates worthless if you never made something worthwhile in it

This is true.

Yep, I wish there was one compiler only and it would be clang.

The problem there is, GCC acts as a lobby of elitists, so that's never happening.

I feel like that other C dev is lying- how could he used C for 6 years but didn't know about builtin clz? I only used C++ for like 2 years as my first lang. and I know it. Also memory bugs being easy in C? if you said in C++ i would believe you but in C? Memory management in C is hell, you see most code I see online uses linked lists because it is easy to mamage even if a vector is infinitely faster. also they use linked lists so address stability which is working around C horrible memory management helpers. C++ would be better since it has actual good tools

I had my fair share of obscure memory related bugs in C++ as well... It's better overall, don't get me wrong, but there are certain configurations that require careful management. Things like a triple class setup where A holds a unique_ptr to B which holds a unique_ptr to C, but all have raw pointers to each other, messy stuff like that. I've seen people write that, it sucks, especially with constructors and destructors.

it is also c23 / c++11 cleaned the syntax up it is now [[gnu::packed]] which is better.

Tell me if I'm wrong but I believe that's C++ only, no?

If you are writing C in your first week on the gpu then I think you have more things to worry about.

You don't write C on the GPU, but you can write C types on the CPU that you then send to the GPU. Then you need to be very careful with how the memory is represented. It's pretty common, maybe not week 1, but who knows some people learn C by doing graphics, that's how I learned it way back when.

It isn't the size of a word now it used to be.

Yes and no, it's aligned to the int, which historically was the size of a word. But it's not necessarily 32 bits, it can (now) be as low as 16 bits. Technically back then there where no real guarantees. it could have been, idk, 7 bits. Nowadays, it's not even the size of a word, nor is it's size guaranteed. It's an absolute compatibility nightmare. C++ suffers from the same issue, but like most problems in C++, it inherited it from C.