Rust

6761 readers
17 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
51
77
Announcing Rust 1.84.1 (blog.rust-lang.org)
submitted 2 months ago by [email protected] to c/rust
52
21
Terminal clock (github.com)
submitted 2 months ago by fil to c/rust
 
 

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

53
 
 

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.

54
 
 

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

55
56
10
submitted 2 months ago* (last edited 2 months 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.

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.

57
22
Rust Analyzer Changelog #269 (rust-analyzer.github.io)
submitted 2 months ago by snaggen to c/rust
58
 
 
59
60
25
submitted 2 months 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.

61
 
 

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?

62
22
submitted 2 months ago by KillTheMule to c/rust
 
 

This article very much conveys what I think.

63
64
 
 

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

65
 
 

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

66
56
Announcing Rust 1.84.0 (blog.rust-lang.org)
submitted 3 months ago by [email protected] to c/rust
67
 
 

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

68
69
 
 

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.

70
 
 

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? :)

71
 
 

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

72
6
submitted 3 months ago* (last edited 3 months 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.

73
12
submitted 3 months ago by [email protected] to c/rust
 
 

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

74
16
struct in Rust (lemmy.ca)
submitted 3 months 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.

75
 
 

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 :)

view more: ‹ prev next ›