Coming from a bash scripting background, Rust’s syntax is mind boggling. The code from HttpServer...
to await?
is a single object. Trying to figure out how it works is going to be my first task.
I will return later with results in the replies.
// Create Http server with websocket support
let settings_bind = settings.clone();
HttpServer::new(move || {
let context = LemmyContext::create(
pool.clone(),
chat_server.to_owned(),
client.clone(),
activity_queue.to_owned(),
settings.to_owned(),
secret.to_owned(),
);
let rate_limiter = rate_limiter.clone();
App::new()
.wrap(middleware::Logger::default())
.app_data(Data::new(context))
// The routes
.configure(|cfg| api_routes::config(cfg, &rate_limiter))
.configure(|cfg| lemmy_apub::http::routes::config(cfg, &settings))
.configure(feeds::config)
.configure(|cfg| images::config(cfg, &rate_limiter))
.configure(nodeinfo::config)
.configure(|cfg| webfinger::config(cfg, &settings))
})
.bind((settings_bind.bind, settings_bind.port))?
.run()
.await?;
new
isn’t a method andHttpServer
isn’t calling it.new
is an associated function of whateverHttpServer
is. Associated functions called new are usually used to create a new “instance” of a type they are associated with. SoType::new
will usually return aType
, aResult<Type, _>
, aOption<Type>
or something similar.Methods are functions that operate on a type, they always have
self
or&self
or&mut self
or whatever as their first parameter. You can omit that parameter if you use the “dot-syntax”.// So if you have let instance = Type::new(); // You can either write (like an associated function) Type::method(instance, other_parameter); // or (dot syntax) instance.method(other_parameter);
The context (I assume you mean the variable in the closure) isn’t moved anywhere, the
move
keyword means, that the closure will take the ownership of all variables from the outer scope mentioned inside of it instead of borrowing or copying them.What can be destroyed? In most cases you don’t need to destroy (free/uninit) anything in rust, that is done “automatically” when something goes out of scope.
It’s an associated function of
App
.It’s a method of whatever
.bind((settings_bind.bind, settings_bind.port))?
returns.You should really take @nutomic@lemmy.ml’s advice and read the book, play around a little bit and maybe do the rustlings course before you try understanding more complex programs. It’s not super hard but it will take some time, but feel free to ask here or in the rust forums if you feel stuck.