Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "r#" in Rust?

Tags:

rust

This page in the guide for rocket has a piece of code as follows:

use rocket::form::Form;

#[derive(FromForm)]
struct Task<'r> {
    complete: bool,
    r#type: &'r str,
}

#[post("/todo", data = "<task>")]
fn new(task: Form<Task<'_>>) { /* .. */ }

What is the r# in the struct Task?

I know what a raw string literal is in Rust, which begins with r and is immediately bracketed by an arbitrary number of #. It seems however in the above snippet of code there is no closing #.

like image 236
DatoClement Avatar asked Sep 11 '25 15:09

DatoClement


1 Answers

It allows you to escape the reserved word type and use it as a field of the struct. You can find more about 'raw identifiers' here.

like image 54
Mário Queiroz Avatar answered Sep 14 '25 09:09

Mário Queiroz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!