DEV Community

Cover image for SORRY, RUST & DEVS !
Bek Brace
Bek Brace

Posted on

SORRY, RUST & DEVS !

Hi!
Today, I will be comparing - for the second time: C and Rust in terms of speed to count till 500M.
But before we jump in, let's address something real quick.

Sorry, Folks!

Last time I talked about C and Rust, I got hit with a wave of comments – some of them pretty passionate, I might add. Seems like I missed the mark with some of the code examples.
Some comments were pretty harsh - here, on Twitter and on YouTube, and somewhat lacked politeness - My advise, if you want to correct someone, do it with humbleness and respect.
In general, your feedback is precious (for the majority of adequate readers / viewers), and I'm here to set things straight.
So, grab a cup of coffee, and let's dive back in.

1- C

C is 50 years old !
It's like that vintage car your grandpa swears by – reliable, sturdy, but not without its quirks.
Take a look to my first test in C:

#include <stdio.h>
#include <time.h>

int main() {
    clock_t start_time = clock();

    unsigned long long count = 0;
    unsigned long long target = 500000000;

    while (count < target) {
        count++;
    }

    time_t end_time = time(NULL);
    double elapsed_time = difftime(end_time, start_time);

    printf("Counting to %llu took %f seconds.\n", target, elapsed_time);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

The count ended in ... well, take a look to the video :)

Now, I am not a savvy in neither C nor Rust, but I have a pretty solid foundation in C at least, and C's like that buddy who's been with you through thick and thin.
C is your best school friend, but you gotta watch your feet: Manual memory management? Yep, that's on you [and here Rust wins!].
And don't even think about safety nets like preventing data races or dangling reference errors, It's a bit of a rollercoaster, but hey, sometimes you need that freedom.

2- Rust

Now let's me talk a bit about Rust in this contest.
It's like C's younger sibling who's always preaching about safety and memory management :)) But you know what? It's got some cool tricks up its sleeve.
Here's Rust counting to 500M, I have used cargo run --release [which was way faster than with rustc command]

use std::time::{Instant};

fn main() {
    let start_time = Instant::now();

    let mut count: u64 = 0;
    let target: u64 = 500_000_000;

    while count < target {
        count += 1;
    }

    let elapsed_time = start_time.elapsed().as_secs_f64();

    println!("Counting to {} took {} seconds.", target, elapsed_time);
}
Enter fullscreen mode Exit fullscreen mode

Rust might seem a bit intimidating at first, but it's got some serious game. With features like ownership and lifetimes [tutorials made, but not yet uploaded on the channel], it keeps you from shooting yourself in the foot with null pointers and data races.
Also the result of the Rust speed test was shocking !

Finding Your Groove

So, which one should you go for? Well, it depends on what floats your boat. If you're all about tradition and doing things your way, C might be your jam. But if you're ready to embrace the future and prioritize safety and reliability, Rust might just be your new bestie.

At the end of the day, it's not about which language is better. It's about finding the right tool for the job and being open to new ideas and perspectives. So, whether you're rocking C, Rust, or something entirely different, remember that we're all in this together.

Your Feedback Matters

I wanna hear from you, folks!
What's your take on C and Rust?
Any cool stories or experiences to share?
Drop 'em in the comments below, and let's keep this conversation rolling.

Thanks for reading this article.
Until next time, happy coding! 🚀

Top comments (0)