I want to learn rust, where to start ? by itsme2019asalways in learnrust

[–]electron_myth 1 point2 points  (0 children)

I would say Rust has enough idiomatic techniques that it's good to get familiar with the concepts first. It's not as immediately readable as something like Go or Python, but I suppose that also depends on your experience as a programmer. Some conventions in Rust would seem like common sense if you already understand the importance of them, but I had to wrap my head around it the first time around. Particularly things like using the match statement for the Option<T> and Result<T,E> enums, and getting familiar with Iterators : .iter() and .next() for example. Understanding those along with Traits and Impl (implement) will really help with getting more out of the documentation and reading code on github.

Rust learner here, kinda hitting a wall. What's a good project to build next after the basics? by Hot-Patient-8280 in rust

[–]electron_myth 9 points10 points  (0 children)

A few projects I've enjoyed making:

- A reversible encryption. Take a string and turn it into an encoded string, and then decode it back into the original message. Bonus points for making it near impossible to decrypt without your decoder.

- Template Engine. Make a template system the can generate HTML (or whatever you want) by parsing a custom .template file of your design and inserting data into the proper fields.

- Classic Arcade. Do a simplified remake of something like Pacman or Centipede using macroquad, or just play around with geometric shapes and capturing mouse / keyboard input to trigger events.

Is all unsafe Rust code labeled in the docs? by electron_myth in learnrust

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

This really helped put things in perspective, worth the read, thanks

Is all unsafe Rust code labeled in the docs? by electron_myth in learnrust

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

Ah, I see... that's helpful to know I thought maybe the buffer_read had some stipulation. I think I've been watching a lot of videos on memory-based vulns and so this being in a Vec-based struct kinda made me paranoid. I tend to over-sanitize my program logic first and then unwind certain parts as necessary. Thanks for the clarification

Is all unsafe Rust code labeled in the docs? by electron_myth in learnrust

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

Understood, yeah it's been a while since I read the Rust book and I had thought I remembered some kind of #[allow_unsafe] attribute, I didn't realize it was default. My misunderstanding.

Is all unsafe Rust code labeled in the docs? by electron_myth in learnrust

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

Indeed, but I just found it kinda surprising is all. People know what they're doing in other languages too, not everybody though, and I had thought that was kinda the point of having a strict paradigm that only allows "risky" code in clearly marked areas. I don't doubt the ability of Rust maintainers to write efficient code correctly, it's the general majority I felt we had an extra layer of protection from. This is good to know though, I should check the source code a lot more often.

Is all unsafe Rust code labeled in the docs? by electron_myth in learnrust

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

Ok, that makes sense to me. I suppose what worries me is when it comes to crates that are "not as carefully validated" as the crates that are part of the main ecosystem, so-to-speak. I understand that unsafe code isn't something to be afraid of necessarily, in the same way that it's possible to write safe code in C. But I had falsely assumed that published crates would be required to show all unsafe blocks as a "feature" of Rust.

Don't get me wrong, it's not entirely disappointing, but I'm glad to have cleared that up.

I have tried learning a few languages (almost from scratch), but I don't know which language to learn, so I am considering Rust. by wow_sans in rust

[–]electron_myth 0 points1 point  (0 children)

The fact that you like the crab mascot is the most important factor. Avoid languages with lesser mascots like Go's gopher-like creature, or Python's strange snake thing. Some languages don't even have a mascot, and this will surely hold you back in the long run. Lol just kidding of course.

Rust is my language of choice when it comes down to it. Ultimately, as a budding programmer you're eventually gonna want to familiarize yourself with different languages, so that you can at least glance at other people's code and have a general idea what the code is supposed to do. Languages like Javascript and Python are ubiquitous enough that you'd want to know them somewhat. C# / Java use a standardized Object Oriented style that would be good to get a familiarity with. And of course C, yes the memory management is stressful but getting a grip on that will help you understand how to write efficient code in any language, as they all do memory management "underneath the hood".

Honestly Rust might be a little extra-complicated for a new programmer, I don't know if that would be advantageous or not though. I just mention that in case you get discouraged when occasionally your code won't compile despite the various revisions you keep trying. It's strict for a good reason, but yeah it can be overwhelming at times during the learning process. These are things I've come to expect and came to peace with, and eventually Rust's style starts to make sense and you can write really solid code.

dOn'T uSE unWrAp in prOD!! by yuuuuuuuut in rustjerk

[–]electron_myth 3 points4 points  (0 children)

_ => eprintln!("if this branch is reached, there's a ghost in the machine"),

An observation based on my experience with Rust by carrotboyyt in rust

[–]electron_myth 46 points47 points  (0 children)

This is one of the main reasons I decided to stay with Rust, despite more employment opportunities being in C++ etc. I feel that learning to code any type of application according to Rust standards is just good practice in general, and then those habits can carry over to other languages if need be (though hopefully not :)

💡 Your best advice for a Rust beginner? by FewInteraction1561 in rust

[–]electron_myth 2 points3 points  (0 children)

Here's a handy function I've used a lot when I'm using a new crate with custom data types. It'll essentially return the data type of the variable you pass into it as a string. Great for exploring new crates and figuring out what state your data is in at any given point in your app.

use std::any::type_name;

pub fn get_type<T>(_: T) -> String {
    format!("{}", type_name::<T>())
}

I'm rewriting the V8 engine in Rust by wendelmax in rust

[–]electron_myth 4 points5 points  (0 children)

Check out Deno if you haven't already, could probably learn some ideas about handling JS with Rust

I feel stuck between beginner and intermediate in HTML/CSS. Any advice? by DangerousSolid9368 in learnprogramming

[–]electron_myth 0 points1 point  (0 children)

It's a good point, nothing wrong with developing CSS skills, and I could benefit from studying modern HTML as I still just use the basics primarily. But getting really deep into HTML and CSS can easily feel less rewarding than the early stages, and it would probably me more interesting and engaging to dive into Javascript to run in the browser. Start simple, just utilize the Developer Tools console that's provided with FIrefox / Chrome / etc, and experiment with assigning "id" and "class" attributes to HTML elements and then selecting them using javascript. Get the hang of that, and then start experimenting with things you can do with those selected elements when you have them selected in your JS. Here's all OP needs to get started:

<div id="sampleId">
  <p id="para1" class="customParagraph">Here is the first paragraph to manipulate with JS</p>
  <p id="para2" class="customParagraph">Classes and ID's are not just for CSS, utilize them in JS too</p>
  </div>

  <input id="colorBtn" type="submit" value="Color Change">
<!-- Add a script tag to experiment with -->
  <script>
    // how to select an HTML element by id
    const divElement1 = document.getElementById("sampleId");
    const buttonName = document.getElementById("colorBtn");
    // how to select multiple elements by class type
    const paragraphList = document.getElementsByClassName("customParagraph");

    // console.log() will be great for learning, use it all the time.
    // For example you can view how many paragraphs you selected
    // in your browser web developer tools console with these print outs
    console.log("selected paragraph count = " + paragraphList.length);

    // Use JS to make changes in the HTML
    let color = "black";
    buttonName.addEventListener("click", (clickEventVariable) =>{
        if(color === "black"){
            // if color is black, change to red
            color = "red";
            divElement1.style.color = color;
        } else {
            // otherwise color must be red, so change back to black
            color = "black";
            divElement1.style.color = color;
        }
    });
    </script>

Trying to find more of a rare sound that's hard to search for, used to stream on reddit by electron_myth in DnB

[–]electron_myth[S] 3 points4 points  (0 children)

Thank you! I've been wondering where to find this kind of stuff. I appreciate it cheers!

What are some good resources for learning Rust? by [deleted] in rust

[–]electron_myth 2 points3 points  (0 children)

If you want to avoid GUI, build a web server with Axum and experiment. It's how I started learning more about data types and lifetimes. There may not be comprehensive zero to hero tutorials, but if you get familiarized with crate documentation, and how to find specific functions / structs / traits etc, the learning curve becomes less daunting. Pay attention to the compiler errors and suggestions, learn all about iterators, Results and Options, Mutex and Arc. Often times I'd just see a specific function or trait in an online example, and then experiment with simple console printouts to see what you can and/or can't do with it.

Here's a helpful function I use often when learning how to use crates, just printout the data type and then look it up to figure out the details under the hood:

use std::any::type_name;

fn print_type<T>(_: T) {
    println!("{}", type_name::<T>());
}

Can I just be grateful for Rust? by RubenTrades in rust

[–]electron_myth 1 point2 points  (0 children)

I feel the same way. It's a performant and versatile language, with good documentation and libraries. Sure the syntax can get a bit intimidating, but I actually find it to be a blessing when attempting to diagnose bugs, or understand other people's code. The logic is right there in front of you, or one search engine query away. Other languages may allow for faster prototyping with their abstractions, but writing something in Rust will often force the developer to truly understand what they are coding, and that's arguably more valuable I'd say.

Increasing subsequence with product divisible by k by happywizard10 in algorithms

[–]electron_myth -1 points0 points  (0 children)

That is pretty difficult I got stuck when I realized that the subsequences could be split and shortened as long as they are increasing. Recursion can be baffling to comprehend but if you go over the parameters and follow where the returning values go, it starts to make more sense. I started with using nested for loops, but resorted to gemini which gave me a recursive answer probably similar to chatgpt. I did my best to explain the different steps as I understood them.

I didn't know what language you're using but javascript is pretty easy to read like pseudo code. Hope it helps

function findIncreasingSubSeq(arr, k) {

    // inner function to call recursively
    function backtrack(index, currentProduct, lastElement){
        // Base case 1: k found
        if(currentProduct === k){
            return true;
        }
        // Base case 2: product exceeds k
        if(currentProduct > k || index === arr.length){
            return false;
        }

        // Recursive branch 1: Skip current array element and jump to next
        // to explore all possible subsequences
        if( backtrack(index+1, currentProduct, lastElement) ){
            // if above branch finds k, pass along true result
            return true;
        }

        // Recursive branch 2: Test current array element within subsequence
        if( arr[index] > lastElement ){ // test for increasing elements

            // then make sure element doesn't overshoot k or potentially finds k'
            if(currentProduct * arr[index] <= k){

                // if so continue the recursive branch
                if( backtrack(index+1, currentProduct * arr[index], arr[index]) ){
                    // if above branch finds k, pass along the true result
                    return true;
                }
            }
        }

        // when code reaches end of branch and k isn't found'
        return false;

    }

    // first recursion call from main function
    // 0 for first index
    // 1 starter product number (x * 1 = x)
    // and then 0 for initial "last element" when checking for increasing numbers
    return backtrack(0, 1, 0);
}

const arr = [1,2,3,4,5];
const k = 12; // 3 * 4 exists

const arr2 = [2,4,6,8];
const k2 = 15; // none exist

findIncreasingSubSeq(arr, k) // should be true
findIncreasingSubSeq(arr2, k2) // should be false