cannot call non-const fn open::<&str> in statics calls in statics are limited to…
- User
cannot call non-const fn
open::<&str>in statics calls in statics are limited to constant functions, tuple structs and tuple variants consider wrapping this expression inLazy::new(|| ...)from theonce_cellcrate: https://crates.io/crates/once_cellrustcClick for f extern crate rocket; use rocket::*; use rocket::fs::FileServer; extern crate rocket_dyn_templates; use rocket_dyn_templates::{Template, context}; extern crate sqlite3; static mut DB: sqlite3::Connection = sqlite3::open("db.sqlite3").unwrap();#[get("/")] fn index() -> Template { Template::render("index", context!{}) }
#[get("/new")] fn new_view() -> Template { Template::render("new", context!{}) }
// #[get("/api/new?&")] // fn new(title: &str, content: &str) -> String {
// }
#[launch] fn rocket() -> _ { unsafe { DB .execute("create table if not exists articles (title text, content text)") .unwrap(); } build() .attach(Template::fairing()) .mount("/", routes![index, new_view, /new/]) .mount("/static", FileServer::from("static")) }
- Assistant
The error message is indicating that you cannot call a non-constant function (
open::<&str>) in a static context.To resolve this issue, you can use the
once_cellcrate to create a lazy static variable. This way, theopen::<&str>function will be called only once and the result will be stored in the static variable.Here's an example of how to fix the issue using
once_cell:extern crate rocket; use rocket::*; use rocket::fs::FileServer; extern crate rocket_dyn_templates; use rocket_dyn_templates::{Template, context}; extern crate sqlite3; use std::lazy::SyncLazy; use once_cell::sync::Lazy; static DB: Lazy<sqlite3::Connection> = Lazy::new(|| { sqlite3::open("db.sqlite3").unwrap() }); #[get("/")] fn index() -> Template { Template::render("index", context!{}) } #[get("/new")] fn new_view() -> Template { Template::render("new", context!{}) } #[launch] fn rocket() -> _ { unsafe { DB .execute("create table if not exists articles (title text, content text)") .unwrap(); } build() .attach(Template::fairing()) .mount("/", routes![index, new_view]) .mount("/static", FileServer::from("static")) } - User
#[get("/api/new?&")] fn new(title: &str, content: &str) -> Redirect { let db = sqlite3::open("db.sqlite3").unwrap(); let mut statement = db .prepare(format!("select * from articles where title = '{}'", title)) .unwrap(); if let State::Row = statement.next().unwrap() { return Template::render("error", context! { e: "Article alreadt exists" }) } db .execute(format!("insert into articles values ('{}', '{}')", title, content)) .unwrap(); Redirect::to("/") } throws error[E0308]: mismatched types --> src/main.rs:26:16 | 20 | fn new(title: &str, content: &str) -> Redirect { | -------- expected
Redirectbecause of return type ... 26 | return Template::render("error", context! { e: "Article alreadt exists" }) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expectedRedirect, foundTemplate - Assistant
The error message indicates that the type of the value being returned from the
newfunction is expected to beRedirect, but you are returning aTemplateinstead.To fix this error, you should return a
Redirectvalue instead of aTemplate. You can use theRedirect::to()function to create aRedirectvalue that redirects the user to a specific path.Here's how you can modify the
newfunction to fix the error:#[get("/api/new?<title>&<content>")] fn new(title: &str, content: &str) -> Redirect { let db = sqlite3::open("db.sqlite3").unwrap(); let mut statement = db .prepare(format!("select * from articles where title = '{}'", title)) .unwrap(); if let State::Row = statement.next().unwrap() { return Redirect::to("/error"); // Redirect to the error page } db.execute(format!("insert into articles values ('{}', '{}')", title, content)) .unwrap(); Redirect::to("/") // Redirect to the home page } - User
translate this from C to rust: malloc(1024*1024)
- Assistant
In Rust, you can allocate memory dynamically using the
Box::newfunction. To translatemalloc(1024*1024)from C to Rust, you can use the following code:let size = 1024 * 1024; let buffer = Box::new([0u8; size]);In Rust, the
Box::newfunction is used to allocate memory on the heap and initialize it with the specified value, which in this case is an array ofu8(unsigned 8-bit integers) with all elements set to zero. The size of the buffer is1024 * 1024bytes, equivalent to 1 megabyte (MB).Note that in Rust, memory is deallocated automatically when the
buffervariable goes out of scope, so you don't need to explicitly free the memory like you do in C withfree(). The Rust compiler automatically inserts the necessary code to free the memory when it's no longer needed.