Hello,
In D it's possible to do this:
#!/usr/bin/env dub
/+ dub.sdl:
name "hello_vibed"
dependency "vibe-d" version="~>0.8.0"
+/
void main()
{
import vibe.d;
listenHTTP(":8080", (req, res) {
res.writeBody("Hello, World: " ~ req.path);
});
runApplication();
}
(Example from dlang.org front page)
Meaning, the dependency is specified in the source code itself, and the entire project can consist of just one file. There is also a shebang to make it executable and runnable as a script.
Is it possible to do this in Rust/Cargo, i.e. avoid needing to create a Cargo.toml
, src
directory etc.?
You can make 1-file programs when you compile them with rustc
directly, but they won't have access to any dependencies unless you install them and specify them on the command line yourself.
You can avoid the src directory by setting [[bin]] path=
in Cargo.toml
But you can't avoid Cargo.toml if you want dependencies handled for you automatically. Cargo and rustc are separate tools, and Cargo chose not to parse Rust code.
Thank you, that is great. (I guess it's only fitting that it was written by a former D developer, heh.)
However, I can't get it to work when adding a second file:
hello.rs:
pub fn hello() {
println!("Hello, world!");
}
test.rs:
mod hello;
fn main() {
hello::hello();
}
rustc
works fine, but run-cargo-script test.rs
fails with:
Compiling test v0.1.0 (file:///home/.../.cargo/.cargo/script-cache/file-test-1a393d922911dcf6)
error[E0583]: file not found for module `hello`
--> test.rs:1:5
|
1 | mod hello;
| ^^^^^
|
= help: name the file either hello.rs or hello/mod.rs inside the directory ""
error: aborting due to previous error
For more information about this error, try `rustc --explain E0583`.
error: Could not compile `test`.
In D, this works with the rdmd
tool. Is there a Rust equivalent, or am I doing something wrong?