What Rust can learn from PHP?

This is not meant to start a holy language war. I'm curious if there is something that Rust can learn from the "convenience" of PHP, or are all these "convenience" a result of untyped code and thus can't work in Rust.

Here are the observations:

  1. To start a PHP project, all I need is a server with PHP support, edit a *.PHP file -- and BAM, I can start iterating.

  2. With Rust, my current setup involves AWS Lambda. So the process is something like (2a) install musl libc, (2b) figureout how to compile Rust to run on Lambda, (2c) configure some AWS lambda end points [this is not Rust's fault], and (2d) deal with either rocket or actix to get routing working, and (2e) finally start writing my REST handler.

  3. FB has also released "hack", which I'm not familiar with, but seems to suggest one can bolt types on to PHP.


Now, my question -- is it plausible that we one can build an Apache/Nginx "mod_rs", where one just drops some *.rs file in a directory -- and start iterating against it as a REST endpoint.

This would be like PHP, except *.rs instead of *.php files.

If the answer is "no, this can't be done" -- I'm curious what technical limitations prevent this from being done.

If the answer is "yes, this can be done" -- I'm curious on why this is a bad idea / hasn't been done yet.

I mean, if you're just dropping .rs files into a directory, when should it compile them? On every access? That's what php does, more or less.


I did not consider this. What makes most sense? Probably some type of directory watcher that recompiles on drop, and on access, just runs it?

I guess each file would also need a "embedded Carto.toml" -- either that, or some way to specify a "Cargo.toml" that works for all *.rs files in a given sub directory.

Honestly I can't say the 1. is just BAM and trivial. I remember the first time I configured nginx for my own server from literally zero knowledge, and it takes entire weekend to setup fairly basic structure - register certs, redirect http to https, serve statics, and forward /api/* requests to given port with tls termination. If you feel the 1. is easy and comfortable, I suspect you just get used to all these tasks. You can also be used to those Rust + lambda setup at some point. If it's because the "server" setup is done by other people/team/service, sure one can also make a service which takes .rs files and setup lambda for you.


What one might consider, which I don‘t know if possible at all is to provide a REPL for Rust. This is quite common approach for several languages as you immediately can check out simple code constructs with immediate feedback. Several languages comes with a REPL (ReadEvaluatePrintLoop) which somes often in handy for example to learn how maps, iterators and other things work without the need for a complete project setup. The RLS (RustLanguageServer) maybe enable this kind of feature?

Rust REPL, why not?

1 Like

Please keep in mind, that PHP was originally planned as a templating language used from other tools. Only social pressure and time has made it to what it is today, a general purpose object oriented language that is used mainly on the web.

This is contrary to rust, which is planned and designed as a general purpose language as a better C and C++ (simply spoken). Only social pressure is what wants to suit Rust into the web.

Basically the main difference is that with rust you do not only need to code your web application, but also the HTTP server that runs it.

Said that, there are a lot of questions about how you would do it in rust.

The syntax leaves no room for PHP like embedding of code, but instead you would need to return some or write into mutable arguments that represent your reply. Similarly the request would need to be passed as an argument.

I really can't make my mind how to do the routing, module loading and dependencies in such an environment...

Perhaps it's possible with a limited subset of rust, but I really think it's just not worth the effort.

Just use rocket or whatever and deploy it behind a reverse proxy. This is state of the art about everywhere in the industry, except for PHP, and even there is fpm what tries something similar to avoid recompilation all the time, but again hidden behind plugins that make it appear as if PHpP is better supported then everything else...


At work we have a simple php file that runs a rust executable and uses passthru to output the stdout of the Rust code. This is rather simple to set up!


If you're comfortable sharing, I'd like to hear more about this. To get started:

  1. Is there a single long living rust "server" that handles all these requests, or is a rust program fired up, have it run, and killed for every request ?

  2. For a language like Rust, is there a big cost to "launch a new linux process" on every request?

  3. How is PHP / Rust communicating -- json/serde ?

  4. This is kinda interesting, having Rust doing the "computational heavy lifting" and Php doing the "glue code" -- what is it like in practice ?

The Rust code performs a rather expensive computation related to automated personel scheduling. The communication happens more or less by php giving it a job id as a command line parameter, and the rust code the uses diesel to read and write the problem data from a database.

The cost of starting a new process for every request is negligible compared to the cost of the computation, and happens pretty rarely (as in less than once per day).

Some log output is also written to stdout, and php uses passthru to transfer it as the text response of the web request.


For ease of deploy, I'd suggest looking into the now.sh builders for AWS lambda, but apparently rust is no longer supported first-party! Looking at the forked out community-supported builder may still be informative however.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.