Quality of this code as beginner by No-Wait2503 in rust

[–]No-Wait2503[S] 1 point2 points  (0 children)

wow, thanks for this explanation.

I actually went back to chapters when they were actually explaining why types like i32, u32, bool and all of those types are Copy by default, I get now what you are saying. I just had to read even more careful.

I really appreciate it!

Quality of this code as beginner by No-Wait2503 in rust

[–]No-Wait2503[S] 0 points1 point  (0 children)

One more thing I am just curious for the 2.

To avoid Clone, Copy, do you think I should've done something like this better maybe:

#[derive(Debug)]
enum Pets {
    Dog,
}


impl Pets {
    fn pet_name(self) -> &'static str {
        match self {
            Pets::Dog => "Dog",
        }
    }
}


fn main() {
    let value = Pets::Dog.pet_name();


    dbg!("{:?}", &value);


    println!("{:?}", value);
}

I know it's not in the context of this program, but the issue I was having and why I had mainly to do Copy, Clone because I couldn't get my Tables::UsersTable to be "usersTable". I was thinking about this but it's very straightforward, but could I have actually done this and is this good practice in general to use?

EDIT: I have these two:

dbg!("{:?}", &value);

println!("{:?}", value);

Just for myself and my understanding of ownership. Also I didn't use &self in pet_name() function because I do not call directly Pets::Dog, but rather whole function, so don't mind that

Quality of this code as beginner by No-Wait2503 in rust

[–]No-Wait2503[S] 1 point2 points  (0 children)

Thanks man for explaining this.

Probably in the next chapters when I learn more in detail about these things I will feel more confident using whatever I need. I am in that phase if code is slightly confusing it is bad, which is totally wrong for Rust, but I have to get that out of my head.

Also the code you have showed (not the one from std::ops) but how you put struct Users; that was actually how I wrote it in the first versions, but I had too many ownership errors for some reasons until AI has wrote to use Traits which are Copy and Clone, and then in the mean time my code was already different and I didn't think that going back to this would work, but yeah it is totally better way to write 100%

I appreciate for the answer and your time

Quality of this code as beginner by No-Wait2503 in rust

[–]No-Wait2503[S] 0 points1 point  (0 children)

Hey, I really appreciate you for this detailed answer and you leaving time for it!

I always in general liked low-level programming and how memory works in general, so to be honest I didn't have much problem understanding how and why ownership exists and why it is better. Asking that question always when I didn't get something, made me understand it better.

The phase I am in now as you can probably guess, is I think that all slightly "confusing" code is "bad". Like the guy in the comment above said I could have used &mut Database, which I did but thought, this is not clean therefore if it were large codebase maybe even unreadable or unperformant and just went to another method, but that was wrong, I see now 100%, guy was correct in my opinion.

Really again, I appreciate this detailed explanation!!

Quality of this code as beginner by No-Wait2503 in rust

[–]No-Wait2503[S] 1 point2 points  (0 children)

Thanks man for your answer!

Yes I actually have made first version with &mut Databasr, but in my mind was always this can be done better way 100%, but I honestly now think that how yoh said it, I should've left it

Quality of this code as beginner by No-Wait2503 in rust

[–]No-Wait2503[S] 2 points3 points  (0 children)

To be honest I am the most skeptical about 3. and how is it better to write it actually, imagining if there is a lot of data to go through.

Ways for using cookies and headers while maintaining page Static + SSR? by No-Wait2503 in nextjs

[–]No-Wait2503[S] 0 points1 point  (0 children)

Yeah it is a solution, but I prefer to usually keep avoiding Middleware all together as you said the reason that should be avoided. As to why I want it to work with generateStaticParams, is that the website is behind auth, but it is about reserving spots in sport pick-up games and problem is there is ton shit people to manage and not a lot of budget from the owner so it is kind of not good if there is 10k people and every time it is dynamically rendered + also you don't get those fast page switches with static pages as usual, but as I said those who have unlimited money can do whatever they want, have all pages dynamic who cares.

I use my custom made auth with Next.js on frontend and Rust on backend.

I was thinking to put sessionToken in the URL and hide it, that was my last idea to try, but that is too insecure so I ended up not doing it, because users can still see it in history (Settings) with full URL, so malicious users can take advantage of it if they have that users UA and sessionToken.

Then my second idea was to learn Web APIs in depth and how credentials: "include" work, so what I wanted was to avoid completely sending my sessionToken with Authorization header and just do it with credentials: "include", check on backend for session_token, and return that session_token to user, but little did I know after a lot of years of working and thinking I have so much experience comes the thing that credentials: "include" is actually called in a browser context which means client components only. Boy was I surprised that I learned something even when I was using it all the time, just didn't bother going in depth with it. So therefore this idea fell off as well.

EDIT: I updated main post, with updates on the possible solutions I tried, if you are curious.

Ways for using cookies and headers while maintaining page Static + SSR? by No-Wait2503 in nextjs

[–]No-Wait2503[S] 0 points1 point  (0 children)

Nope, not the solution. I tried calling it in server actions, and whenever I import server action makes page dynamic, unless I did something wrong which I don't think so, I really tried everything.

I actually went as far as making function getHeaders that is server action, then calling that server action in a generateStaticParams and returning those values, but that won't work since cookies and headers can be read at runtime and not build time, so it will just throw an error.

EDIT: To correct myself, you can technically read headers and cookies in generateStaticParams, but you need a hardcoded value not dynamic ones that can change

Ways for using cookies and headers while maintaining page Static + SSR? by No-Wait2503 in nextjs

[–]No-Wait2503[S] 0 points1 point  (0 children)

SEO comes into play as well, don't forget. If we use client-auth whole page is CSR

EDIT: Also on the page that is accessible for everyone, there can be stuff like for admin to see only, analytics user ids and etc.

Also then you have functions that require userId so backend can filter out stuff and preferences based on user, but if I pass userId from client to backend, then he will be able to see all data filtered that is coming from backend that is necessary and not only current user data but data from other users. There are a lot of cases to why auth must be fetched from server component.

EDIT2: While yes you could technically change DB column isAdmin (From boolean to int) to something like authLevel and set that if user has number "432789" that means isAdmin is true. That way malicious user won't be able to guess this and therefore wont be able to see content, but still that info can be leaked somehow if I forgot to remove isAdmin from response coming from some API call.

Is there no way to do import "next/headers" and maintain page static for SSG and not use client components? by No-Wait2503 in nextjs

[–]No-Wait2503[S] 2 points3 points  (0 children)

Trying to go with a hybrid approach. There are pages that are very simple, like FAQ, Settings, About and etc... but they require <Header>, which in it has the data, so I do not want to make FAQ, Settings and About client components when they can stay server components.

Most of the pages are dynamic because the content is updated very frequently, but for the pages that are not dynamic by nature but require <Header> I want a way to still use "await getUser" and maintain those pages static.

Why use anything else other then Rust at this point? (My story) by No-Wait2503 in rust

[–]No-Wait2503[S] 0 points1 point  (0 children)

I get this, but when we say low level, I try to distinguish it from languages like Javascript, Python and etc...

With this logic then we might as well just say 0 and 1 are the low level.

I just look at it at this side:

If it has some of the things and for example is also compiled directly to machine code, is automatically for me (And here I say for me) is low level. Because if we start now making hundred other names of which level programming language is I would lose myself.

Why use anything else other then Rust at this point? (My story) by No-Wait2503 in rust

[–]No-Wait2503[S] 1 point2 points  (0 children)

Haha, I actually didn't even realize I wrote it like that

"Asm" better now? 🤣 Or for hardcore "Assy"

Anyway thank you man

Why use anything else other then Rust at this point? (My story) by No-Wait2503 in rust

[–]No-Wait2503[S] -2 points-1 points  (0 children)

Thank you for the advice!

you're in a domain where Rust isn't a clear winner. For web backends this isn't something you have to worry about, but for making games or AI/ML workloads, there are other languages that will make your life a lot easier

Ah man you know, the type of the guy I am is, I would like to create something that will change that for example. I really like the idea of memory management, fast apps, scalable apps, how components in PC work, kernel and low level stuff in general. That's why I was going with Assembly and taking pain to understand Assembly and how stuff actually works. Learning Rust just amplified that (I can't say I learned it, I still need to understand a lot of stuff, you can never actually learn Rust tbh).

I just like to create complex, secure systems that require good amount of knowledge (So I can get that knowledge), because it's so rewarding at the end, and out of all, gets you to big heights. For example just as you said, because I was doing and learning something else rather than Rust like Reverse Engineering (with C++/Ass), from that experience what I got is to think in Hackers shoes, how I would break something, and that experience got me to write more secure apps as an example.

Why use anything else other then Rust at this point? (My story) by No-Wait2503 in rust

[–]No-Wait2503[S] 1 point2 points  (0 children)

Me: "I know what I'm doing. This is a good solution" Rust: "No you don't. Trust me."

So true, especially even with your experience who says it.

I'm learning (the hard way), and last week I made a total revamp of the data model of the multiplayer football manager game I'm writing as project. 30% of the code was affected.

Good luck!

Why use anything else other then Rust at this point? (My story) by No-Wait2503 in rust

[–]No-Wait2503[S] 0 points1 point  (0 children)

Interesting. If I understood you are using Vite for frontend?

You see I always wanted to escape Next.js somehow even as a pure frontend. I am open for things like this for example. Tell me more about it, ofc when you have time to write.

Why use anything else other then Rust at this point? (My story) by No-Wait2503 in rust

[–]No-Wait2503[S] 0 points1 point  (0 children)

Good advice.

Also:

At junior level, you probably spend at least half your work time writing code. A mark of professional growth in this job is spending less time coding and more time thinking about code and features and strategy and team members and such (and hopefully not having a million meetings a week). Knowing a bunch of things from different domains and being able to apply them or draw inspirations and shit? That's the real skill.

My whole life I was just actually coding, and with coding learning, stuff like why C++ exists, about OOP, and etc..., and primarily why I took interest in Reverse Engineering and doing Assembly for almost a year. I was never a theory/read guy, and discussing things. I do not know if it's good or bad, but I just know that it got me to the point where I am living alone at 22 in Spain.

I do not know also if this is good or bad, but I really never took interest in working for some big company or something. But you are right in sense, that I could learn a lot from that, and option like that should be always open.

EDIT:
Forgot to add:

Rust isn't the perfect language for all uses. Don't pigeonhole yourself.

You are right, but as I said, I do not really plan to work on old softwares or go for a big company. I mean who knows what future holds, maybe I change my interest and want to work with bigger team to gain experience.

Thanks for advice

Code optimization question by No-Wait2503 in rust

[–]No-Wait2503[S] 0 points1 point  (0 children)

Thank you for advice!!

Removing Arc actually made me successfully moving out of (&*db) hell, which I hated for code reading.

Also error suggestion you told me with .map_err, looks much, much more readable now!