What does &* combined together do in Rust?

In short: the * triggers an explicit deref, which can be overloaded via ops::Deref. More Detail Look at this code: let s = “hi”.to_string(); // : String let a = &s; What’s the type of a? It’s simply &String! This shouldn’t be very surprising, since we take the reference of a String. Ok, but what … Read more

Why do I get “identifier is undefined” or “not available” when inspecting a Rust variable in the VSCode debugger?

This issue is not reproducible with recent release of CodeLLDB. CodeLLDB v1.7.0 rust : 1.60.0 (7737e0b5c 2022-04-04) vscode: v1.67.0 Everything works as expected the debug view shows iter: {end:0x000055555559105f} c: ‘t’ Upgrading the codelldb to specified version will resolve the issue.

Is it possible to represent Higher-Order Abstract Syntax in Rust?

As a fan of lambda calculus I decided to attempt this and it is indeed possible, though a bit less sightly than in Haskell (playground link): use std::rc::Rc; use Term::*; #[derive(Clone)] enum Term { Hol(Box<Term>), Var(usize), Lam(Rc<dyn Fn(Term) -> Term>), App(Box<Term>, Box<Term>), } impl Term { fn app(t1: Term, t2: Term) -> Self { App(Box::new(t1), … Read more

How to read a struct from a file in Rust?

Here you go: use std::io::Read; use std::mem; use std::slice; #[repr(C, packed)] #[derive(Debug, Copy, Clone)] struct Configuration { item1: u8, item2: u16, item3: i32, item4: [char; 8], } const CONFIG_DATA: &[u8] = &[ 0xfd, // u8 0xb4, 0x50, // u16 0x45, 0xcd, 0x3c, 0x15, // i32 0x71, 0x3c, 0x87, 0xff, // char 0xe8, 0x5d, 0x20, 0xe7, … Read more

How do I create a heterogeneous collection of objects?

Trait objects The most extensible way to implement a heterogeneous collection (in this case a vector) of objects is exactly what you have: Vec<Box<dyn ThingTrait + ‘static>> Although there are times where you might want a lifetime that’s not ‘static, so you’d need something like: Vec<Box<dyn ThingTrait + ‘a>> You could also have a collection … Read more

How can I use a module from outside the src folder in a binary project, such as for integration tests or benchmarks?

Here’s a literal answer, but don’t actually use this! #![feature(test)] extern crate test; #[path = “../src/foo.rs”] // Here mod foo; #[bench] fn bencher(_: &mut test::Bencher) { println!(“{:?}”, foo::Thang); } In fact, it’s very likely that this won’t work because your code in foo.rs needs supporting code from other files that won’t be included. Instead of … Read more

Can I destructure a tuple without binding the result to a new variable in a let/match/for statement?

Yes. The Rust team has published a new version of Rust 1.59.0 in Feb. 24, 2022, you can now use tuple, slice, and struct patterns as the left-hand side of an assignment. Announcing Rust 1.59.0 Destructuring assignments You can now use tuple, slice, and struct patterns as the left-hand side of an assignment. let (a, … Read more