Rust

7243 readers
4 users here now

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

Wormhole

!performance@programming.dev

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
126
127
 
 

Jiff is a datetime library for Rust that encourages you to jump into the pit of success. The focus of this library is providing high level datetime primitives that are difficult to misuse and have reasonable performance.

And as a user of Jiff, I must say that it is very nice to use. Well thought out API, making date time handling less of a pain. So, nice work @burntsushi@programming.dev

128
129
130
131
19
submitted 6 months ago* (last edited 6 months ago) by jasory@programming.dev to c/rust@programming.dev
 
 

I wrote up a port of GNU factor that has a slightly nicer UI than the original, and runs in approximately 1/3rd the time for 128-bit integers, on average. This is just a preliminary release and I plan implementing elliptic curve arithmetic and extending it to 192-bit to cover all the small integers that CADO-NFS doesn't support.

The factorization algorithm is provided as a separate crate that provides a C-api, since fast factorisation algorithms are hard to come-by.

132
133
 
 

Greetings,

sometimes I want to read new comments on a particular post but I don't want to check it every hour to see new comments. It would be nice if I can just say "!remind 5h" so that I get a reminder to checkout a specific post which would have new comments after 5 hour.

I am thinking about making it myself in Rust, but if there is already a bot here and I don't know, please tell me.

134
71
Announcing Rust 1.84.1 (blog.rust-lang.org)
submitted 6 months ago by neme@lemm.ee to c/rust@programming.dev
135
 
 

I've made a tui clock. Ratatui now is so nice to use compared to 1 year ago!

136
 
 

Inspired by Contex-Generic Programming, I made this macro, that allows you to write those generic implementations easily. This is kinda CGP, but simple and with better errors.

Ever repeat yourself implementing traits? Or wanted to easily swap out parts of your code, with (almost) no need to refactor? That is what this is for.

137
 
 

yeehaw is a TUI framework with a full event routing system and plethora of built in elements.

Highlights:

  • Actual Neovim can be used as a text editor element (or any other editor set in the $EDITOR environment variable). Likewise, any other TUI that outputs to the terminal can be integrated into an element.
  • The color system includes an alpha channel for transparency. A color can also be set to gradients (position or time-based). Basically a yeehaw color can effectively be a neopet's rainbow paintbrush if you want it too.
  • Element ownership at a glance: TUI Elements are organized in a hierarchical manner while each retaining autonomy to change their ordering and position within their immediate parent. Events (keyboard/mouse/custom) are routed top down and responses propagate back upwards along the Element ownership tree. Simple elements are only normally required to have spatial awareness within the confines provided to them.
  • The core element Trait allows for customizable event/response types enabling developers to create entirely new sub-classes of elements which can reuse the event routing system.

This project is not yet entirely stable, however it felt necessary to unleash this monstrosity into the wild at this point. Feel free to ask any questions here or on github. Also this is my first rust release, so please forgive (and inform me of) missing idioms. -bogz

138
139
10
submitted 6 months ago* (last edited 6 months ago) by Doods to c/rust@programming.dev
 
 

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.

EDIT: I FIGURED IT OUT!!!

My mistake was measuring the size of "target/release", I discovered that that folder contains many "unnecessary" files, like "deps", which greatly bloat the folder'r size, the actual size of my binary is 864K.

I am so relieved.

140
141
 
 
142
143
 
 

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.

144
 
 

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?

145
 
 

This article very much conveys what I think.

146
 
 

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

147
 
 

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

148
53
Announcing Rust 1.84.0 (blog.rust-lang.org)
submitted 7 months ago by neme@lemm.ee to c/rust@programming.dev
149
 
 

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();
}

150
view more: ‹ prev next ›