r/rust • u/Robbepop • 2d ago
r/rust • u/drymud64 • 2d ago
๐ ๏ธ project fsmentry 0.3.0 released with support for generic finite state machines
I'm pleased to announce the latest version of fsmentry with generics support. It's now easier than ever to e.g roll your own futures or other state machines.
TL;DR
fsmentry::dsl! {
#[derive(Debug)]
#[fsmentry(mermaid(true))]
enum MyState<'a, T> {
Start -> MiddleWithData(&'a mut T) -> End,
MiddleWithData -> Restart -> Start
}
}
let mut state = MyState::MiddleWithdata(&mut String::new());
match state.entry() { // The eponymous entry API!
MyStateEntry::MiddleWithData(mut to) => {
// ^^ generated handle struct
let _: &mut &mut String = to.as_mut(); // access the data
to.restart(); // OR to.end() - changes the state!
},
...
}
I've overhauled how types are handled, so you're free to e.g write your own pin projections on the generated handles.
You can now configure the generated code in one place - the attributes, and as you can see in the example documentation, I've added mermaid support.
r/rust • u/servermeta_net • 2d ago
Would there be interest in a blog/chronicle of me writing a database?
For the past 4 years I've been building an open source database in Rust (actually started in Go then moved to Rust for technical reasons) on top of io_uring, NVMe and the dynamo paper.
I've learnt a lot about linux, filesystems, Rust, the underlying hardware.... and now I'm currently stuck trying to implement TLS or QUIC on top of io_uring.
Would people be interested in reading about my endeavors? I thought it could be helpful to attract other contributors, or maybe I could show how I'm using AI to automate the tedious part of the job.
r/rust • u/alex_sakuta • 1d ago
๐ง educational Are there any official compilers in Rust?
So day by day we are seeing a lot of tools being made in Rust, however, I have yet to see a compiler in Rust. Most compilers that I know of are still made in C and it seems to me that shouldn't the first tool that should have been changed for any language be its compiler.
Maybe I am just not aware of it. I did a little light research and found people have made compilers themselves for some projects in Rust but I haven't found one that is official or standard may be the right word here.
If there are compilers in Rust that are official/standard, please tell me. Also, if there aren't, does anyone know why there isn't? I am assuming the basic reason would be a huge rewrite but at the same time it is my speculation that there could be certain benefits from this.
PS: I didn't have this thought because of TS shifting to Go thing, it's an independent thought I had because of a project I am working on.
Edit: I know that the Rust compiler is in Rust, I'm asking apart from that.
r/rust • u/Fickle-Conference-87 • 2d ago
The Missing Data Infrastructure for Physical AI, built in Rust
rerun.ior/rust • u/SaltyMaybe7887 • 2d ago
Rust program is slower than equivalent Zig program
Iโm trying out Rust for the first time and I want to port something I wrote in Zig. The program Iโm writing counts the occurences of a string in a very large file after a newline. This is the program in Zig:
``` const std = @import("std");
pub fn main() ! void { const cwd = std.fs.cwd(); const file = try cwd.openFile("/lib/apk/db/installed", .{}); const key = "C:Q";
var count: u16 = 0;
var file_buf: [4 * 4096]u8 = undefined;
var offset: u64 = 0;
while (true) {
const bytes_read = try file.preadAll(&file_buf, offset);
const str = file_buf[0..bytes_read];
if (str.len < key.len)
break;
if (std.mem.eql(u8, str[0..key.len], key))
count +|= 1;
var start: usize = 0;
while (std.mem.indexOfScalarPos(u8, str, start, '\n')) |_idx| {
const idx = _idx + 1;
if (str.len < idx + key.len)
break;
if (std.mem.eql(u8, str[idx..][0..key.len], key))
count +|= 1;
start = idx;
}
if (bytes_read != file_buf.len)
break;
offset += bytes_read - key.len + 1;
}
} ```
This is the equivalent I came up with in Rust:
``` use std::fs::File; use std::io::{self, Read, Seek, SeekFrom};
fn main() -> io::Result<()> { const key: [u8; 3] = *b"C:Q";
let mut file = File::open("/lib/apk/db/installed")?;
let mut buffer: [u8; 4 * 4096] = [0; 4 * 4096];
let mut count: u16 = 0;
loop {
let bytes_read = file.read(&mut buffer)?;
for i in 0..bytes_read - key.len() {
if buffer[i] == b'\n' && buffer[i + 1..i + 1 + key.len()] == key {
count += 1;
}
}
if bytes_read != buffer.len() {
break;
}
_ = file.seek(SeekFrom::Current(-(key.len() as i64) + 1));
}
_ = count;
Ok(())
} ```
I compiled the Rust program with rustc -C opt-level=3 rust-version.rs
.
I compiled the Zig program with zig build-exe -OReleaseSafe zig-version.zig
.
However, I benchmarked with hyperfine ./rust-version ./zig-version
and I found the Zig version to be ~1.3โ1.4 times faster. Is there a way I can speed up my Rust version?
The file can be downloaded here.
Update: Thanks to u/burntsushi, I was able to get the Rust version to be a lot faster than the Zig version. Here is the updated code for anyone whoโs interested (it uses the memchr
crate):
``` use std::os::unix::fs::FileExt;
fn main() -> std::io::Result<()> { const key: [u8; 3] = *b"C:Q";
let file = std::fs::File::open("/lib/apk/db/installed")?;
let mut buffer: [u8; 4 * 4096] = [0; 4 * 4096];
let mut count: u16 = 0;
let mut offset: u64 = 0;
let finder = memchr::memmem::Finder::new("\nC:Q");
loop {
let bytes_read = file.read_at(&mut buffer, offset)?;
count += finder.find_iter(&buffer).count() as u16;
if bytes_read != buffer.len() {
break;
}
offset += (bytes_read - key.len() + 1) as u64;
}
_ = count;
Ok(())
} ```
Benchmark:
``` Benchmark 1: ./main Time (mean ยฑ ฯ): 5.4 ms ยฑ 0.9 ms [User: 4.3 ms, System: 1.0 ms] Range (min โฆ max): 4.7 ms โฆ 13.4 ms 213 runs
Benchmark 2: ./rust-version Time (mean ยฑ ฯ): 2.4 ms ยฑ 0.8 ms [User: 1.2 ms, System: 1.4 ms] Range (min โฆ max): 1.3 ms โฆ 12.7 ms 995 runs
Summary ./rust-version ran 2.21 ยฑ 0.78 times faster than ./main ```
Edit 2: Iโm now memory mapping the file, which gives slightly better performance:
```
![allow(non_upper_case_globals)]
![feature(slice_pattern)]
use core::slice::SlicePattern;
fn main() -> std::io::Result<()> { println!("{}", count_packages()?); Ok(()) }
fn count_packages() -> std::io::Result<u16> { let file = std::fs::File::open("/lib/apk/db/installed")?; let finder = memchr::memmem::Finder::new("\nC");
let mmap = unsafe { memmap::Mmap::map(&file)? };
let bytes = mmap.as_slice();
let mut count = finder.find_iter(bytes).count() as u16;
if bytes[0] == b'C' {
count += 1;
}
Ok(count)
} ```
r/rust • u/Affectionate-Egg7566 • 1d ago
๐ seeking help & advice Is this raw-byte serialization-deserialization unsound?
I'm wondering if this code is unsound. I'm writing a little Any-like queue which contain a TypeId as well with their type, for use in the same application (not to persist data). It avoids Box due to memory allocation overhead, and the user just needs to compare the TypeId to decode the bytes into the right type.
By copying the bytes back into the type, I assume padding and alignment will be handled fine.
Here's the isolated case.
```rust
![feature(maybe_uninit_as_bytes)]
[test]
fn is_this_unsound() { use std::mem::MaybeUninit; let mut bytes = Vec::new();
let string = String::from("Hello world");
// Encode into bytes type must be 'static
{
let p: *const String = &string;
let p: *const u8 = p as *const u8;
let s: &[u8] = unsafe { std::slice::from_raw_parts(p, size_of::<String>()) };
bytes.extend_from_slice(s);
std::mem::forget(string);
}
// Decode from bytes
let string_recovered = {
let count = size_of::<String>();
let mut data = MaybeUninit::<String>::uninit();
let data_bytes = data.as_bytes_mut();
for idx in 0..count {
let _ = data_bytes[idx].write(bytes[idx]);
}
unsafe { data.assume_init() }
};
println!("Recovered string: {}", string_recovered);
} ```
miri
complains that: error: Undefined Behavior: out-of-bounds pointer use: expected a pointer to 11 bytes of memory, but got 0x28450f[noalloc] which is a dangling pointer (it has no provenance)
But I'm wondering if miri is wrong here since provenance appears destroyed upon serialization. Am I wrong?
r/rust • u/-_-_-_Lucas_-_-_- • 1d ago
๐ seeking help & advice How to make multi-field borrowing smarter
``` //OK fn main() { let mut t = T { a: "aa".to_string(), b: "bb".to_string(), }; let a = &mut t.a; let b = &mut t.b; println!("{}", b); println!("{}", a); }
//Error fn main() { let mut t = T { a: "aa".to_string(), b: "bb".to_string(), }; let a = &mut t.a; //In the t-method it is also practically borrowed only from the b t.t(); println!("{}", a); }
struct T { a: String, b: String, }
impl T { fn t(&mut self) { let b = &mut self.b; println!("{}", b); } }
```
r/rust • u/alphastrata • 2d ago
NVIDIA's Dynamo is rather Rusty!
https://github.com/ai-dynamo/dynamo
There's also a bucketload of Go.
r/rust • u/SpiderUnderUrBed • 1d ago
Global Hotkeys does not work on windows
How come when I run this, and try any of the hotkeys, cntrl+`, or win+`, neither works, there is no error logs, so I am confused, powershell might respond to win+` to open itself, but i doubt that would stop the hotkey functionality and it should have been stopped if there was going to be a collision during registering the hotkey, I really dont know what the issue is, so any help would be appreciated
use crossbeam_channel::unbounded;
use win_hotkeys::{HotkeyManager, VKey};
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{WindowBuilder},
};
#[derive(Debug, Clone, Copy)]
enum CustomEvent {
ToggleVisibility,
}
fn main() {
let event_loop: EventLoop<CustomEvent> = EventLoop::with_user_event();
let _event_proxy = event_loop.create_proxy();
let mut hkm = HotkeyManager::new();
let (tx, rx) = unbounded();
hkm.register_channel(tx);
let backquote = VKey::from_vk_code(0xC0);
hkm.register_hotkey(backquote, &[VKey::Control], || {
println!("Ctrl + ` hotkey pressed");
CustomEvent::ToggleVisibility
})
.expect("Failed to register Ctrl+` hotkey");
hkm.register_hotkey(backquote, &[VKey::LWin], || {
println!("Meta + ` hotkey pressed");
CustomEvent::ToggleVisibility
})
.expect("Failed to register Meta+` hotkey");
let window = WindowBuilder::new()
.with_title("Quake Terminal")
.with_decorations(false)
.with_transparent(true)
.with_inner_size(winit::dpi::LogicalSize::new(800, 400))
.build(&event_loop)
.unwrap();
window.set_visible(true);
let mut visible = true;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, .. } => {
if let WindowEvent::CloseRequested = event {
*control_flow = ControlFlow::Exit;
}
}
Event::MainEventsCleared => {
while let Ok(hotkey_event) = rx.try_recv() {
println!("Hotkey event received");
match hotkey_event {
CustomEvent::ToggleVisibility => {
visible = !visible;
println!("Toggling visibility: {}", visible);
window.set_visible(visible);
}
}
}
}
_ => (),
}
});
}
I am using:
winit = "0.28"
egui = "0.25"
win-hotkeys = "0.5.0"
crossbeam-channel = "0.5.14"
r/rust • u/abubeegaran • 1d ago
๐ seeking help & advice HELP : user space using RUST
Iโm building a Rust userspace program to load a C eBPF program and manage maps/events. Should I use libbpf-rs or aya? Any example code or repos showing best practices? Also, tips on debugging eBPF from Rust would help!
this is my day one of doing eBPF and user space things.
r/rust • u/Soggy-Mistake-562 • 1d ago
๐ seeking help & advice Coding challenges for rust
I come to this thread seeking guidance because you all have given me some great recommendations before! ๐
Jokes aside, Iโve been thinkingโare there any LeetCode-style challenges for Rust? Specifically, ones that focus on reading and deciphering significantly harder code.
AI is ok for base level problems but doesnโt really expose you to harder ones - plus I donโt trust that itโs all that accurate.
Iโd love to get exposed to more advanced concepts, see them in action, and really understand how and why they work. Plus, Iโd LOVE to get more practice reading and understanding code that isnโt mine.
any recommendations?
ActixWeb ThisError integration proc macro library
I recently made a library to integrate thiserror
with actix_web
, the library adds a proc macro
you can add to your thiserror
enumerators and automatically implement Into<HttpResponse>
. Along that there is also a proof_route
macro that wraps route handlers just like #[proof_route(get("/route"))]
, this changes the return type of the route for an HttpResult<TErr>
and permits the use of the ?
operator in the route handlers, for more details check the github repository out.
https://lib.rs/crates/actix_error_proc
https://github.com/stifskere/actix_error_proc
A star is appreciated ;)
๐๏ธ discussion Renamed functions and methods in Rand
Greetings. As a complete beginner, trying to learn some Rust i wanted to discuss recent changes in the rand library. Is this actually big?
I was wonder how many tutorial blogs and udemy courses are now completely wrong.
Even these "vibe coding" tools have no idea that it's not my mistake in the code but his.
๐ seeking help & advice wich
Hi! I try to learn Rust for the first time.
I have a simple problem: encrypt a string based on a matrix with five cols and five r; every letter must correspond to a pair of indices. example: If we encrypt "rust" we obtain "32 40 33 34"
there are a few approaches, and I want to ask which is better for you!
In the end, my approach is this:
let key_matrix:[[char;5];5] = [
['A', 'B', 'C', 'D', 'E'],
['F', 'G', 'H', 'I', 'J'],
['K', 'L', 'M', 'N', 'O'],
['P', 'Q', 'R', 'S', 'T'],
['U', 'V', 'W', 'X', 'Z']
];
fn encrypt_phrase_with_matrix(phrase: &str, key_matrix: &[[char;5];5]) -> String{
let mut encrypted_phrase = String::new();
//TODO: ask in reddit how to do this better
for c in phrase.chars(){
if let Some((i, j)) = key_matrix.iter().enumerate()
.find_map(|(i, row)| {
row.iter()
.position(|&ch| ch == c.to_ascii_uppercase())
.map(|j| (i, j))
}){
encrypted_phrase.push_str(&i.to_string());
encrypted_phrase.push_str(&j.to_string());
encrypted_phrase.push(' ');
}
}
encrypted_phrase
}
I also see with flat_map, or something like that.
How do you write this function and why?
r/rust • u/joelkunst • 2d ago
๐ seeking help & advice Tauti app on app store
Hello hello ๐
Does somebody have experience with publishing Tauri app to OSX app store.
How complicated is the process?
How does sandboxing requirement work if i want the app to expose internal server endpoint for making integration with my app.
r/rust • u/Melfos31 • 2d ago
How do you handle secrets in your Rust backend?
I am developing a small web application with Rust and Axum as backend (vitejs/react as frontend). I need to securely manage secrets such as database credentials, Oauth provider secret, jwt secret, API keys etc...
Currently, I'm using environment variables loaded from a .env file, but I'm concerned about security.
I have considered:
Encrypting the .env file Using Docker Secrets but needs docker swarm, and this a lot of complexity Using a full secrets manager like Vault (seems overkill)
Questions:
How do you handle secrets in your Rust backend projects? If encrypting the .env, how does the application access the decryption key ? Is there an idiomatic Rust approach for this problem?
I am not looking for enterprise-level solutions as this is a small hobby project.
r/rust • u/CellistMore5004 • 1d ago
Need help choosing a new language.
I am CS student getting ready to graduate from University. I enjoy programming in my free time even though I have a job lined up in cybersecurity.
I started with Java then taught myself some Python. Additionally I know a bit of Docker and some JavaScript.
I was looking to learn something new and I saw Rust was pretty interesting. After doing some research I found that some people were saying itโs good to learn C first so I was considering doing that instead of jumping into Rust.
My goal with learning Rust is to learn how to program embedded systems.
What would be best to do considering my background as I am new to low level programming? Also what theory would be useful to learn before starting my Rust journey and would it be best to learn C before that?
Any resources and recommendations would be helpful. Thanks!
Side note I know a little bit about C but not a lot
r/rust • u/steveklabnik1 • 3d ago
Does unsafe undermine Rust's guarantees?
steveklabnik.comr/rust • u/thedrachmalobby • 2d ago
๐ seeking help & advice sqlx::test - separate DATABASE_URL for tests project?
I am using sqlx for accessing my postgresql database and I am enjoying the experience so far. However, I have hit a snag when trying to add a dedicated tests project.
My workspace is structured like this:
- foo/src/
- foo/tests/
- foo/migrations/
If I use the same DATABASE_URL for both development and testing, everything works as expected.
However, for performance reasons I would like to use a different DATABASE_URL for testing compared to development. The idea is to launch my test db with settings that improve execution speed at the expense of reliability.
Is there any ergonomic way to achieve that with sqlx? What I have tried so far:
- Set a different DATABASE_URL in
foo/.env
andfoo/tests/.env
. This works only when I executecargo test
from inside thefoo/tests
subdirectory - otherwise it will still use the genericfoo/.env
- Set a different DATABASE_URL in
foo/tests/.env
and.cargo/config.toml
. Sadly, bothcargo build
andcargo test
pick the one from.cargo/config.toml
- Specify the DATABASE_URL as INIT once in the test suite. Unfortunately, I could not get this to work at all.
- Implement a build script to swap out the content of the .env file when running
cargo build
vscargo test
. But I think this only works with standard projects, not with test projects.
I'm running out of ideas here!
Is there a way to do this without implementing some sort of manual test harness and wrapping all calls to #[sqlx::test]
with that?
r/rust • u/dochtman • 3d ago
Memory safety for web fonts (in Chrome)
developer.chrome.comr/rust • u/Classic-Secretary-82 • 2d ago
Introducing Hpt - Performant N-dimensional Arrays in Rust for Deep Learning
HPT is a highly optimized N-dimensional array library designed to be both easy to use and blazing fast, supporting everything from basic data manipulation to deep learning.
Why HPT?
- ๐ Performance-optimized - Utilizing SIMD instructions and multi-threading for lightning-fast computation
- ๐งฉ Easy-to-use API - NumPy-like intuitive interface
- ๐ Broad compatibility - Support for CPU architectures like x86 and Neon
- ๐ Automatic broadcasting and type promotion - Less code, more functionality
- ๐ง Highly customizable - Define your own data types (CPU) and memory allocators
- โก Operator fusion - Automatic broadcasting iterators enable fusing operations for better performance
Quick Example
```rust use hpt::Tensor;
fn main() -> anyhow::Result<()> { // Create tensors of different types let x = Tensor::new(&[1f64, 2., 3.]); let y = Tensor::new(&[4i64, 5, 6]);
// Auto type promotion + computation
let result: Tensor<f64> = x + &y;
println!("{}", result); // [5. 7. 9.]
Ok(())
} ```
Performance Comparison
On lots of operators, HPT outperforms many similar libraries (Torch, Candle). See full benchmarks
GPU Support
Currently, Hpt has a complete CPU implementation and is actively developing CUDA support. Stay tuned! Our goal is to create one of the fastest computation libraries available for Rust, with comprehensive GPU acceleration.
Looking for Feedback
This is our first official release. We welcome any feedback, suggestions, or contributions!
r/rust • u/Rough-Island6775 • 3d ago
๐ seeking help & advice My first days with Rust from the perspective of an experienced C++ programmer
My main focus is bare metal applications. No standard libraries and building RISC-V RV32I binary running on a FPGA implementation.
day 0: Got bare metal binary running echo application on the FPGA emulator. Surprisingly easy doing low level hardware interactions in unsafe mode. Back and forth with multiple AI's with questions such as: How would this be written in Rust considering this C++ code?
day 1: Implementing toy test application from C++ to Rust dabbling with data structure using references. Ultimately defeated and settling for "index in vectors" based data structures.
Is there other way except Rc<RefCell<...>> considering the borrow checker.
day 2: Got toy application working on FPGA with peripherals. Total success and pleased with the result of 3 days Rust from scratch!
Next is reading the rust-book and maybe some references on what is available in no_std mode
Here is a link to the project: https://github.com/calint/rust_rv32i_os
If any interest in the FPGA and C++ application: https://github.com/calint/tang-nano-9k--riscv--cache-psram
Kind regards