Description
Problem
The documentation https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#script says:
script
Tracking Issue: #12207
Cargo can directly run .rs
files as:
$ cargo +nightly -Zscript file.rs
where file.rs can be as simple as:
fn main() {}
Steps
I just fetched the rust-nightly-x86_64-unknown-linux-gnu.tar.xz
archive, extracted the cargo
folder to the same directory where I have rustc
and rust-std-x86_64-unknown-linux-gnu
folders extracted from the same archive.
I have this for testing
script.rs
use std::env;
// https://stackoverflow.com/a/75981598
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
println!("Hello {}!", &args[1]);
} else {
println!("Hello world!");
}
}
This is result
$ cargo/bin/cargo +nightly -Zscript ~/bin/script.rs
error: no such command: `+nightly`
Cargo does not handle `+toolchain` directives.
Did you mean to invoke `cargo` through `rustup` instead?
Possible Solution(s)
No idea. This is only my second time trying to use Rust.
Notes
It's basically impossible to download the entire Rust toolchain on a temporary file system running a live Linux session.
So I fetched the Nightly archive, immediately got rid of the 600+ MB documentation folder, and everything else except rustc
and rust-std-x86_64-unknown-linux-gnu
folders.
I'll re-fetch the nightly archive and extract cargo
folder.
Eventually I got this to run, using -L
to I guess link to rustlib/x86_64-unknown-linux-gnu/lib
#!/bin/sh
//bin/bash -ec '[ "$0" -nt "${0%.*}" ] && /home/user/bin/rustc/bin/rustc -L /home/user/bin/rust-std-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib "$0" -o "${0%.*}"; "${0%.*}" "$@"' "$0" "$@"; exit $?
use std::env;
// https://stackoverflow.com/a/75981598
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
println!("Hello {}!", &args[1]);
} else {
println!("Hello world!");
}
}
What I'm really working on doing is using Rust .rs
file as a Native Messaging host script.
I converted a working C version to Rust using https://c2rust.com/.
Now I have to figure out how to link to libc
in the resulting file https://gist.github.com/rust-play/92af6fefec0ea8bcc76a5b18be40005e
//https://stackoverflow.com/q/41322300
//https://c2rust.com/
//https://github.com/rust-lang/rust/issues/1772
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
extern "C" {
static mut stdin: *mut _IO_FILE;
static mut stdout: *mut _IO_FILE;
fn fflush(__stream: *mut FILE) -> libc::c_int;
// ...
Just to see what would happen if I was trying to compile the C to Rust code
./rust.sh "test"
error[E0433]: failed to resolve: use of undeclared crate or module `libc`
--> ./rust.sh:10:39
// ...
118 | free(message as *mut libc::c_void);
| ^^^^ use of undeclared crate or module `libc`
error: aborting due to 58 previous errors
For more information about this error, try `rustc --explain E0433`.
This is my second time trying to use Rust to build something. My first time using Rust as a script. Thanks.
Version
cargo 1.82.0-nightly (fa6465836 2024-08-02)
release: 1.82.0-nightly
commit-hash: fa646583675d7c140482bd906145c71b7fb4fc2b
commit-date: 2024-08-02
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/3.3.1)
ssl: OpenSSL 3.3.1 4 Jun 2024
os: Ubuntu 22.4.0 (jammy) [64-bit]