Rust

6242 readers
23 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
1
5
submitted 15 hours ago* (last edited 15 hours ago) by [email protected] to c/rust
 
 

I shaved off 10 MiB from my binary in 2 hours!

I made a program using Macroquad, then I built it in release mode, the binary was 63 MiB in size.

So I used cargo vendor to have a better look at Macroquad and one of its dependencies, glam.

I then started to delete code, like, lots and lots of code(about 30_000 lines of code); none of it affected my main project, some of it became 'dead_code' just by removing the pub keyword.

The result is that my project was unaffected and the binary went down to 52 MiB.

Is there a way to automate removal of unneeded elements from dependencies? This is potentially huge.

2
14
Rust Analyzer Changelog #269 (rust-analyzer.github.io)
submitted 3 days ago by snaggen to c/rust
3
 
 
4
5
23
submitted 5 days ago by secana to c/rust
 
 

The fist new version of Kellnr in 2025 is released! If you want to self-host rust crates on your own infrastructure, check it out.

6
 
 

I've been coming back to the same project a few times. It's essentially just a program that interacts with an API. Only problem is whenever I get back to it, I realize how annoying it is to debug through all the "too many requests" responses I get back from the API because it has a max of 200 requests per second.

On solution would be to filter out those responses but that just feels like the wrong move, so I'm guessing the better solution would be to put some sort of rate limiter on my program. My two questions are: does that seem like a good solution and if it is, do I embed the rate limiter in my program, i.e. using the ratelimit crate or would a better solution be to run my program in a container and connect it to a reverse proxy(I think) container and control rate limiting from there?

7
21
submitted 1 week ago by KillTheMule to c/rust
 
 

This article very much conveys what I think.

8
9
 
 

What I learned about Rust while making pastebin in Rust

First iteration

Hello, I have recently started to make a pastebin to learn more about Rust and understand the underlying concept of Rust so I first made a very naive pastebin where I used axum to serve the files and used a TCPListener to handle file upload. I didn't use axum to handle file upload because I didn't know how to do it, so basically my program was listening to two different port 8080 and 3000 where on port 3000 I served the files and on 8080 I handle file upload using simple TCP connection. I also used a static variable to name the uploaded file, but in Rust mutable static variable considered unsafe since it could lead to race condition, but at that time I didn't know much about Atomic variables so I wraped the code with unsafe.

Second iteration

I uploaded my code of First iteration to Lemmy, and people on Lemmy gave me a lot of suggestions, like using Atomic variable to eliminate the need of unsafe block and using axum to handle file upload. so I implemented that.

Third iteration

there are still some security issue like anyone can scrape entire pastebin since I was using an incremental file name. also if I rerun the pastebin It will reset the file name variable and it would overwrite previously uploaded files, to overcome this issue a person on Lemmy suggested that I should use uuid, that way it would solve those security issue.

Final thoughts

so yeah, that was it, I learned a lot about Rust and programming in general, thank you all on the Lemmy to teach me these cool stuff :D

10
 
 

Logan Smith's Rust videos are excellent - I'm happy to see a new one is up!

11
56
Announcing Rust 1.84.0 (blog.rust-lang.org)
submitted 2 weeks ago by [email protected] to c/rust
12
 
 

Hello, last time I shared my dirty code of pastebin and people suggested me a lot of things so I have implemented those. now the code is reduced to only 42 lines of code :D

last post: https://lemmy.ca/post/36410861

here is the code:

use axum::{extract::Path, routing::get, routing::post, Router};
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::sync::atomic::{AtomicUsize, Ordering};

const MAX_FILE_SIZE: usize = 1024 * 1024 * 10;
static mut FILE_COUNT: AtomicUsize = AtomicUsize::new(0);

async fn handle(Path(id): Path<String>) -> String {
    if let Ok(content) = read_to_string(id) {
        return content;
    }
    return String::from("ERROR: File not found");
}

async fn submit_handle(bytes: String) -> String {
    dbg!(&bytes);
    if bytes.len() > MAX_FILE_SIZE {
        // Don't store the file if it exceeds max size
        return String::from("ERROR: max size exceeded");
    }
    unsafe {
        let path = FILE_COUNT.load(Ordering::Relaxed);
        FILE_COUNT.store(path+1, Ordering::Relaxed);
        let mut output = File::create(path.to_string()).unwrap();
        write!(output, "{}", bytes).unwrap();
        let mut url = String::from("http://localhost:3000/");
        url.push_str(&path.to_string());
        return url;
    }
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(|| async { "Paste something in pastebin! use curl -X POST http://localhost:3000/submit -d 'this is some data'" }))
        .route("/{id}", get(handle))
        .route("/submit", post(submit_handle));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

13
14
 
 

cross-posted from: https://programming.dev/post/23822190

I added this language to my watch list some time ago and forgot about it, until I got a notification about a new release (0.15) yesterday.

I'm someone who is familiar with system languages (C, Rust) and shell languages (Bash, Zsh, ..). But don't have much experience, at a proficient level, with any languages setting in between.

So I gave Koto's language guide a read, and found it to be very well-written, and the premise of the language in general to be interesting. I only got annoyed near the end when I got to @base, because I'm an anti-OOP diehard 😉

I hope this one well start to enjoy some adoption.

15
 
 

Hello,

I was making a pastebin in Rust which I shared to the Rust community but there was one person who asked "why didn't you use upload functionality of axum" (I implemented the upload functionality using by simply using TCP connection). now I think that it might be a good idea to have file upload using axum but I couldn't find how to do it in their documentation. could someone point me to the right direction please? :)

16
 
 

the code I have written isn't very idiomatic or efficient. I am still new to Rust so I am learning things. I am amazed that I can write a pastebin in just 60 lines of Rust code. It's awesome. I am thinking about deploying it on my server.

any suggestions would be appreciated :)

code:

use axum::{extract::Path, routing::get, Router};
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::net::{TcpListener, TcpStream};
use std::str;

const MAX_FILE_SIZE: usize = 1024 * 1024 * 10;
static mut FILE_COUNT: usize = 0;

fn handle_client(stream: &mut TcpStream) -> std::io::Result<()> {
    let mut buf = vec![0; 1024];
    unsafe {
        let file_name = FILE_COUNT.to_string();
        FILE_COUNT += 1;
        let mut file = File::create(file_name)?;
        let mut size: usize = 0;
        loop {
            let read_data = stream.read(&mut buf).unwrap();
            size += read_data;
            if size >= MAX_FILE_SIZE {
                return Ok(())
            }
            if read_data == 0 {
                return Ok(());
            }
            stream.write_all(&buf[..read_data]).unwrap();
            write!(file, "{}", str::from_utf8(&buf[..read_data]).unwrap())?;
        }
    }
}

async fn upload_handle() -> std::io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:8080")?;

    // accept connections and process them serially
    for stream in listener.incoming() {
        handle_client(&mut stream?)?;
    }

    Ok(())
}

async fn handle(Path(id): Path<String>) -> String {
    if let Ok(content) = read_to_string(id) {
        return content;
    }
    return String::from("ERROR: File not found");
}

#[tokio::main]
async fn main() {
    tokio::spawn(upload_handle());

    let app = Router::new()
        .route("/", get(|| async { "Paste something in pastebin!" }))
        .route("/{id}", get(handle));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

17
5
submitted 3 weeks ago* (last edited 3 weeks ago) by [email protected] to c/rust
 
 

There are endless debates online about Rust vs. Zig, this post explores a side of the argument I don't think is mentioned enough.

Intro / TLDR

I was intrigued to learn that the Roc language rewrote their standard library from Rust to Zig. What made Zig the better option?

They wrote that they were using a lot of unsafe Rust and it was getting in their way. They also mentioned that Zig had “more tools for working in a memory-unsafe environment, such as reporting memory leaks in tests”, making the overall process much better.

So is Zig a better alternative to writing unsafe Rust?

I wanted to test this myself and see how hard unsafe Rust would be by building a project that required a substantial amount of unsafe code.

Then I would re-write the project in Zig to see if would be easier/better.

After I finished both versions, I found that the Zig implementation was safer, faster, and easier to write. I’ll share a bit about building both and what I learned.

18
 
 

Hello,

some time ago I shared a Guess game that I made from scratch and this time I thought to do something different so I decided to make a Guess game over IRC.

It's pretty basic but I learned a lot about destructing Enum, unwrap and if let syntax.

I would appreciate any suggestion :)

here is the source code:

use futures::prelude::*;
use irc::client::prelude::*;

use rand::Rng;

#[tokio::main]
async fn main() -> irc::error::Result<()> {
    let config = Config {
        nickname: Some("beep_boop".to_owned()),
        server: Some("irc.libera.chat".to_owned()),
        channels: vec!["#test".to_owned()],
        ..Default::default()
    };

    let mut client = Client::from_config(config).await?;
    client.identify()?;

    let mut stream = client.stream()?;
    let mut secret_number = rand::thread_rng().gen_range(0..101);
    let mut attempts = 0;

    while let Some(message) = stream.next().await.transpose()? {
        print!("{}", message);

        match message.command {
            Command::PRIVMSG(channel, message) => {
                if let Some(command) = message.get(0..6) {
                    if command == "!guess" {
                        let parts = message.split(' ').collect::<Vec<_>>();
                        if let Some(part2) = parts.get(1) {
                            if let Ok(num) = part2.to_string().parse::<u32>() {
                                println!("{:?}", num);
                                if num == secret_number {
                                    client.send_privmsg(&channel, format!("You won with {attempts} attempts! generating next number")).unwrap();
                                    secret_number = rand::thread_rng().gen_range(0..101);
                                    attempts = 0; // reseting the number of attempts 
                                } else if num > secret_number {
                                    client.send_privmsg(&channel, "too high").unwrap();
                                    attempts += 1;
                                } else {
                                    client.send_privmsg(&channel, "too low").unwrap();
                                    attempts += 1;
                                }
                            }
                        }
                    }
                }
                else {
                    continue;
                }
                if message.contains(client.current_nickname()) {
                    client.send_privmsg(channel, "beep boop").unwrap();
                }
            },
            _ => println!("IRC command not implemented: {message}"),
        }
    }

    Ok(())
}

19
16
struct in Rust (lemmy.ca)
submitted 3 weeks ago by [email protected] to c/rust
 
 

Hello,

I learned about struct in Rust, so I just wanted to share what I have learned.

what is struct in Rust? It's the same as what's in C language. eg,

struct Point {
    x: i32,
    y: i32,
}

that's it this is how we define a struct. we can create all sort of struct with different data types. ( here I have used only i32 but you can use any data type you want)

now Rust also have which we find in OOPs languages like Java. it's called method. here is how we can define methods for a specific struct in Rust.

impl Point {
    fn print_point(&self) {
        println!("x: {} y: {}", self.x, self.y);
    }
}

see it's that easy. tell me if I forgot about something I should include about struct in Rust.

20
 
 

hello,

last time I made a fibonacci series generator in Rust and now I have made something different :)

use std::io;

fn main() {
    let mut input: String = String::new();
    let stdin = io::stdin();

    let x = rand::random::<u32>() % 101;
    let mut attempts = 0;

    loop {
        println!("Guess a number from 0 to 100:");
        stdin.read_line(&mut input);
        input = input.to_string().replace("\n", ""); // removing the \n
        let user_input: u32 = input.parse::<u32>().unwrap();
        if x == user_input {
            println!("You won! attempts: {attempts}");
            break;
        }
        else if x < user_input {
            println!("too big");
            attempts += 1;
        }
        else {
            println!("too small");
            attempts += 1;
        }
        input.clear()
    }
}

feel free to give me suggestion :)

21
22
 
 

Hello,

As I said in the previous post that I have started learning Rust and made a simple fibonacci series generator. Today I made a palindrome string checker. it's very basic. I haven't used Enum or Struct in the code since I don't think it's necessary in this simple code.

here is the code:

use std::io;

fn main() {
    let mut input = String::new();
    let stdin = io::stdin();
    stdin.read_line(&mut input).unwrap(); // we want to exit in case it couldn't read from stdin

    input = input.replace("\n", ""); // Removing newline

    let mut is_palindrome: bool = true;
    for i in 0..input.len()/2 {
        let first_char: &str = &input[i..i+1];
        let last_char: &str = &input[input.len()-i-1..input.len()-i];
        if first_char != "\n" {
            if first_char != last_char {
                is_palindrome = false;
            }
        }
    }

    println!("palindrome: {}", is_palindrome);
}
23
21
submitted 1 month ago by kvark to c/rust
 
 

End of the year release with lots of improvements across the board.

24
25
 
 

I've been trying to learn the fuzzing library LibAFL for a while now, but I never seem to be able to fully grasp the essential parts. I've read tutorials, followed along tutorials, read the whole LibAFL book (that's still under construction), and have read a few of the examples in the repo. You could say I'm still in tutorial hell, honestly.

I'm trying to write a simple fuzzer for a malware code sample (MooBot) and I've been trying to figure out two things: how to find the input that has the maximum run time for a function, and how to not run malware directly on my computer. One of them should be more important than the other, but given my lack of expertise in LibAFL right now, I'm focused on the former. For my example, I noticed that there's a custom trim function in MooBot that helps sanitize input:

void trim(char *str)
{
        int i, begin = 0, end = strlen(str) - 1;

    while (isspace(str[begin]))
        begin++;

    while ((end >= begin) && isspace(str[end]))
        end--;

    for (i = begin; i <= end; i++)
        str[i - begin] = str[i];

    str[i - begin] = '\0';
}

This is what I test in my harness. I know I could probably logic my way into finding the input that has the max run time, but I'm using this as an exercise for LibAFL and using the rust FFI. The problem is how to deal with feedbacks and observers. I currently have this with no observers:

let mut feedback = CrashFeedback::new();
let mut objective = CrashFeedback::new();

Which simply reports an input if it crashes the program. It works for inital fuzzing, but now that I'm trying to find an input that maximizes run time this won't work. I tried to figure if there was a maximization feedback that would work with the time observer, but the only feedback that maximizes anything is the MaxMapFeedback which doesn't seem compatible with the time observer.

What I'm envisioning is something like this:

let mut observer = TimeObserver::new();
let mut feedback = MaximizeFeedback::new(&observer);

I think the solution has something to do with MapFeedbacks, but I'm not exactly sure how they work.

view more: next ›