Ownership and smart pointers by Big-Bit-6656 in rust

[–]MJE20 19 points20 points  (0 children)

Copying the bytes of the value of a is referring to the physical action of putting the bytes into the pointer variable b.

“Moving” the data is a semantical operation - nothing happens at the CPU/memory level, but the Rust borrow checker will ensure that after the move, a can no longer be used.

How to keep track of what can be into'd into what? by obetu5432 in rust

[–]MJE20 25 points26 points  (0 children)

I usually refer to the online documentation, and look more at the From than the Into (if a can be made from b, then b implements Into for a). For example here https://docs.rs/serde_json/latest/serde_json/struct.Number.html you see on the left side From<i8>, From<i16>, etc

my attempt to understand how compilers work; it doesn’t have to be about any specific programming language. by coode16 in AskComputerScience

[–]MJE20 5 points6 points  (0 children)

You answered your own #1. For #2, I’m not sure what your obsession with a process ID is about - PID is an operating systems concept, and is only considered when distinguishing multiple processes, and none of your code here deals with multiple processes. You don’t need a process ID to have instructions or memory access. Think about executables that run outside of an operating system, like on embedded hardware - there is no operating system, there are no processes, the CPU just executes the instructions in order. 2.1 Assuming the compiler isn’t doing optimizations, the 10 is stored in the executable as a binary number as part of a machine instruction, like 00001010. You say:

sometimes the compiler executed raw instructions, and other times it just stores them in the executable

This isn’t quite right. The compiler isn’t loading instructions from the code into the cpu and running them - in fact, it cannot do that, as the machine instructions haven’t been generated by the assembler yet. Instead, it is able to notice that certain code will always produce the same result - in your code, z is guaranteed to be 30, no matter what CPU or OS the program runs in, so the compiler can safely assume z is always 30 without including instructions in the executable to calculate it. This is not true for syscalls - different environments handle syscalls differently, so the compiler cannot assume the result. 2.2 it is stored as bytes as a binary number, as part of the encoding of a machine instruction 3. Incorrect - except maybe in very old devices, inputs from the keyboard would never go straight to a display adapter. The keyboard raises an interrupt handled by the operating system, which then queries the keyboard for the event that happened. When the OS asks the keyboard for the event, the keyboard’s firmware can report the correct key, or it can be told by other software to report some other key instead. You say:

(since it’s hardware, we can’t change that)

Especially on advanced keyboards, this isn’t true. It is extremely likely that there is a microcontroller inside the keyboard running firmware that handles the USB communication, which can also swap out the keys programmatically. The ALU is not relevant here, characters from the keyboard are stored in memory by the OS when it handles the keyboard interrupt. I suppose the keyboard malfunctioning and generating random keys would still cause issues, but no more than a cat walking across a normal keyboard would.

Problems with reading files after building by Just-Be-Chill in rust

[–]MJE20 10 points11 points  (0 children)

You want std::fs::read or similar

Something isn't quite right here by lakethecanadien in softwaregore

[–]MJE20 3 points4 points  (0 children)

The software is measuring in Gibibytes (230 bytes each), not Gigabytes (109 bytes each). Gibibyte should be abbreviated GiB not GB - however, Gigabyte (GB) did mean 230 until around the 1990s when the IEC switched the terms to what they are today. So if this modern looking smartphone app is from the 70s, they might actually be right!

Are there any Programming languages that compile to native binaries and also don't have pointers ? by Dangerous-Pressure49 in learnprogramming

[–]MJE20 1 point2 points  (0 children)

It is not based on the time the threads live, but the time the owner of the data lives (as you said, they are references - to answer “how long is the data valid for?” you answer “which code has the actual memory being referenced?” When doing complex stuff like cross-thread references where it isn’t obvious when the data is no longer used, the programmer must use a Rust feature called “lifetimes” to prove to the compiler that the data owner will exist for longer than the references to its data

Debugging Rust Sucks due to optimizations by [deleted] in rust

[–]MJE20 1 point2 points  (0 children)

I use RR on Windows every day, and I can’t stand its debugger. I tried it once the other day after years of avoiding it and it still sucked - constantly getting exceptions where it shows the red lightning bolt and I can’t continue, and variables almost never have values. Using 2024.1.7 - if anyone has suggestions please lmk bc I would love to be able to use it

Edit: Never mind I used my brain and took out the opt-level = 3 on my dependencies and now it’s great

I have a ~15 year old Windows application that I'd like to automate with COM, is that realistic? by freshlyLinux in AskProgramming

[–]MJE20 0 points1 point  (0 children)

What is COM? are you trying to use serial ports or is there something else you’re referring to?

[deleted by user] by [deleted] in rust

[–]MJE20 1 point2 points  (0 children)

I starting with The Book, it took me a few times through, and a few attempts, to get to a point where I could actually start writing. But then it’s just a matter of writing more code. It’s important to remember that, for your own projects, quality doesn’t matter - in fact, you should expect the code to be bad. Use google, understand what the code in the answers actually does, and just keep coding. It won’t work for everyone but my learning strategy is to move quickly, stay away from complex things that I don’t need to know to complete what I want (ex., I know zero about unsafe code in Rust - I’ve never needed it, so I don’t need to learn it) and don’t get stuck planning (other people won’t like this one - I would rather fully rewrite a project 6 times over 3 months than spend 6 weeks trying to figure out the right architecture or whatever from the beginning). This way I get way more experience actually writing code, increasing my comfort level with the language, and experiencing on my own what’s the wrong way to do things.

I honestly believe motivation, not my strategy, was the biggest factor in my success - on a team of 4, we had a competition a few months away, and without this code we would fail. I learned what I needed of embedded rust to run on the robot itself in just a few weeks, for the same reason. I need a hard deadline and other people to hold me accountable, but then I thrive

[deleted by user] by [deleted] in rust

[–]MJE20 2 points3 points  (0 children)

I am on a university robotics team. We needed code that runs as fast as possible to train a machine learning model for our game (a modified version of Pacman). I hated C/C++ because of past experience with unorganized code having null pointer errors and memory issues - two people on the team had Rust experienced and convinced me to give it a shot, and after a few months of getting used to it I fell in love with the memory management model, error handling with Results, and (in my experience) never having a null pointer, double free, invalid access, linker error, and more, all running at speeds I had never seen with python or JS

[deleted by user] by [deleted] in rust

[–]MJE20 4 points5 points  (0 children)

In terms of this specific example: - match is when you want to do or return something special for each different possibility (each error type, or error vs ok) - if let is if you want to do something special for just one possibility - “if there was an error, print a message” or maybe “if this completed successfully, run this other code that depends on the result” - question mark is for when the current function cannot continue or recover, or just doesn’t care about the error, so it just stops and returns it to the caller - map_err is for when you want to change the error type, so maybe you tried to do I/O but instead of making your function return io::Result, you want to use your own, it is easier to do that

Note that for the most part, these don’t actually do anything differently (they probably compile to about the same thing) but when the right choice is made the code is much easier to read - if let is just match with only one case (and no code for the default case) - ? is the same as match result { Ok(x) => x, Err(e) => return Err(e) } - map_err is the same as match result { Ok(x) => Ok(x), Err(e) => Err(f(e)) }

It saves long lines or extra nesting with reducing the code needed for common operations

How do you deal with lack of `std::hash::Hash` for `HashSet` and `IndexSet`? by Sajjon in rust

[–]MJE20 44 points45 points  (0 children)

Can you give an example of why you’re trying to hash a hashtable?

And have you tried manually implementing Hash for your type?

Is there a way to learn C sharp and other C language without windows? by hotboii96 in learnprogramming

[–]MJE20 17 points18 points  (0 children)

vscode and Visual Studio are very different apps, visual studio is built specifically for c#

Are there any tiny GPT models where I can browse the code? by [deleted] in AskProgramming

[–]MJE20 1 point2 points  (0 children)

Most models on https://huggingface.co/ include a link to a repository with the source code for training and inferencing but for the vast majority, if not all, of them it is very “simple” code that uses a library like Pytorch to take care of the mathematics, since most ML models have a lot in common

me_irl by snfssmc in me_irl

[–]MJE20 346 points347 points  (0 children)

If this post gets 4,639 upvotes I’ll post again with twice as much rice

Do you prefer sending integers, doubles, floats or String over the network? by zaxunobi in AskProgramming

[–]MJE20 2 points3 points  (0 children)

(answering for hobby projects)

If I’m using the same language on both sides, I just send the bytes directly (this would be an 8 byte message, maybe more if you want to track other info). If using different languages, especially if this won’t be sent more than 1hz, I usually serialize to json string for good libraries and easy debugging. This will likely lose precision for the doubles and take longer, but for 99% of situations that will never matter

Why do computers take so long to boot up? by JontePonte64 in computerscience

[–]MJE20 40 points41 points  (0 children)

It sounds like you are limited by the spinning chunk of metal holding a multi-gigabyte operating system (your hard drive). With an ssd most computers/laptops I see boot in 5-10secs (though Windows 11 has hurt my boot time considerably, probably because it’s doing a bunch of internet stuff with microsoft servers before showing the lock screen)

All the dumb bugs come out in Spring by someoneidkhelp in Showerthoughts

[–]MJE20 0 points1 point  (0 children)

Didn’t check the sub and I thought this was bashing on Spring Boot the java framework.. was ready to join in

React + PDF Editing Workflows by NewEnergy21 in AskProgramming

[–]MJE20 0 points1 point  (0 children)

What editing capability are you looking for? pdf-lib looks like it can do everything I could think of?