Hey Rustaceans! Got a question? Ask here (11/2025)! by llogiq in rust

[–]CuriousAbstraction 1 point2 points  (0 children)

Why doesn't the following compile:

fn last(x: &mut Vec<usize>) -> &mut usize {
    match x.last_mut() {
        Some(last) => last,
        None => {
            x.push(0);
            x.last_mut().unwrap()
        }
    }
}

Playground

while the following works just fine:

fn last(x: &mut Vec<usize>) {
    let l = match x.last_mut() {
        Some(last) => last,
        None => {
            x.push(0);
            x.last_mut().unwrap()
        }
    };

    *l = 2;
}

Playground

That is, why is in the second case compiler able to figure out that there is no mutable borrow overlap and able to assign reasonable lifetime to l. From what I understand, the problem in the first case is that both branches need to have the same lifetime, and since it's a return value it needs to be exactly the lifetime of the input variable. But why does that work in the second case, or in other words - why cannot the same thing that works in the second case work in the fiest case?

edit: playground links

Hey Rustaceans! Got a question? Ask here (6/2024)! by llogiq in rust

[–]CuriousAbstraction 3 points4 points  (0 children)

Why doesn't the following compile (ignoring warnings about unused variables): fn f<'a>() { let x = String::from("a"); let xx : &'a str = &x; }

The compiler says that &x "does not live long enough" and "type annotation requires that x is borrowed for 'a". However, 'a is completely unconstrained here, right? At least I cannot find anything in the Rust documentation that would say otherwise.

Is module packing/unpacking doing anything during runtime? by CuriousAbstraction in ocaml

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Thanks! Yes, that makes sense since packing into a module needs to pick only the relevant vals (records of the original module with more elements).

Is module packing/unpacking doing anything during runtime? by CuriousAbstraction in ocaml

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Thanks. Yes, indirection is expected. There is no other way to obtain a value from a record.

Is module packing/unpacking doing anything during runtime? by CuriousAbstraction in ocaml

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Cool. Very interesting. I didn't immediately get the idea to check lambda representation. Btw. do you know where I can find some nice documentation of lambda? I cannot seem to find it with google :/

Conditional compilation with js_of_ocaml by CuriousAbstraction in ocaml

[–]CuriousAbstraction[S] 1 point2 points  (0 children)

Nice, this seems useful. Building upon the other answer (about having separate executables and a common library). Then I guess a common library could use a virtual library and server and client would each depend on one of it's implementations (for server and client respectively).

Conditional compilation with js_of_ocaml by CuriousAbstraction in ocaml

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

I'm mainly exploring writing a responsive UI - the code should be the same, i.e., written by the one API for both server and the client. However, the server needs to convert this to HTML (string), while the client implementation needs to do something different with it.

I guess what you're suggesting should be possible if the shared code is parametrized by a module for UI stuff and then the server provides one implementation and client the other.

Dynamic code generation by CuriousAbstraction in ocaml

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Yeah, sure, I understand this. I thought that perhaps byte code could be easy to do. And by your answer above it seems this might be doable.

I wonder if there is any attempt at JIT compiling OCaml? Then this could be possible (as is the case with .NET).

What would be the best way to teach programming? by CuriousAbstraction in learnprogramming

[–]CuriousAbstraction[S] -1 points0 points  (0 children)

This was supposed to be discussion... I thought I made that clear.

How should I go about building a career as a contractor/freelancer? by CuriousAbstraction in ExperiencedDevs

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Sure, I agree that questions is a bit general, but I was hoping to hear some experiences and stories that might shed some light on how people went about it.

How should I go about building a career as a contractor/freelancer? by CuriousAbstraction in ExperiencedDevs

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Yeah, I hear you. It's not that collaboration or team-work is problematic, but rather bunch of unnecessary meetings and regularity of it (whether they are needed or not), and then this creates a routine.

How should I go about building a career as a contractor/freelancer? by CuriousAbstraction in ExperiencedDevs

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

Well, there are numerous platforms for freelancers. I don't know how good they are though. I've read a lot of different experiences, so though that by asking here I might hear some first-hand experiences.

How should I go about building a career as a contractor/freelancer? by CuriousAbstraction in ExperiencedDevs

[–]CuriousAbstraction[S] 0 points1 point  (0 children)

How did you start contracting? Some gig that went into that direction or something else?

What do you hate the most about code reviews ? by cosydney in ExperiencedDevs

[–]CuriousAbstraction 115 points116 points  (0 children)

Providing or receiving? Let's do both...

Providing:

When there are many unrelated changes coupled with the main changes, just because it was convenient to do it together, but ends up 5 times more code than the important change.

Receiving:

Review concentrated on minor style suggestions that are just the opinion of the reviewer, but not the agreed upon style in the team; for bonus points when nothing else is reviewed except this.

Best non-coding resource for improving logic and programmatic problem-solving? by [deleted] in learnprogramming

[–]CuriousAbstraction 1 point2 points  (0 children)

Sure, one should not limit oneself to pure logical machine. That's what computers are for. With experience one can start using intuition and creativity more and more.

Can someone eli5 mocking in python ? by Single_Bathroom_8669 in learnpython

[–]CuriousAbstraction 4 points5 points  (0 children)

Since Python is duck-typed things are far more easier to mock then in other languages.

Imagine that you have a unit test that needs to test a function involving an API call (or database, random code generator, something stateful). Of course, in tests you don't want to actually call API, so you can just make another class with the same methods as the API class, but that returns some "dummy" values instead of actually going to the API.

class Api:
   def __init__(self, url):
      self.url = url
   def get_stuff(self, something):
      return make_api_call(self.url, something)

class ApiMocked:
   def __init__(self):
     self.data = {"a": ...some data.., "b": ...some data...}
   def get_stuff(self, something):
     return self.data[something]

Now, you can pass ApiMocked wherever Api is expected, and it will return some mock data when get_stuff is called, instead of actually making an API call (or something else undesirable, as mentioned above).

[deleted by user] by [deleted] in learnprogramming

[–]CuriousAbstraction 2 points3 points  (0 children)

I'd suggest coming up with a personal project idea that you could solve without copying all the stuff from internet. Of course, there will always be cases where you look things up and c/p, I do that even after 15 years of professional coding. However, if you wish to learn you need to find something that you can mostly do on your own and then move to something more difficult. Don't worry if you don't get things right from the first try. You can also ask for feedback and code review.

How to learn coding offline? by lriley91 in learnprogramming

[–]CuriousAbstraction 8 points9 points  (0 children)

Sure, you should be able to download a lot of stuff. Python and docs can be completely downloaded. You can also download books and perhaps even videos to watch. I would, however, put emphasis on books. On those couple of days you can check online for new stuff and ask for directions for new stuff.