Crates.io | cargo-oj |
lib.rs | cargo-oj |
version | 0.7.0 |
source | src |
created_at | 2023-09-30 13:51:20.959291 |
updated_at | 2024-05-22 08:08:19.886562 |
description | Cargo-OJ: package a crate into single file, minimized and formatted |
homepage | https://github.com/Bubbler-4/rust-problem-solving/tree/main/cargo-oj |
repository | https://github.com/Bubbler-4/rust-problem-solving/tree/main/cargo-oj |
max_upload_size | |
id | 988648 |
size | 40,255 |
All online judges (OJs) require you to write and submit a single file. But keeping all the I/O and algorithm snippets and copy-pasting them into the main file is not only cumbersome, but also against the spirit of modular code.
cargo-oj
lets you keep your code organized in a module structure inside a library crate,
and it will happily pack your lib.rs
and its children into a main.rs
,
removing all the unnecessary bloat (which is dead code) along the way.
Create a library crate.
Write your solution in lib.rs
, which should contain a pub fn main() {...}
. Optionally put
#![allow(dead_code)]
#![allow(unused_imports)]
at the top of lib.rs
. This will eliminate warnings about unused items (mostly data structures and algorithms) in your IDE.
cargo-oj
strips these declarations before testing for dead code.
Write your own library as child modules of lib.rs
. Mark all items that you will directly use as pub(crate)
.
This lets you use those items in different modules within the same crate (especially lib.rs
),
while letting rustc
emit unused warnings when appropriate.
When your solution is complete, run cargo oj
at the crate root folder. src/bin/main.rs
will be created.
Then you can:
cargo run --bin main --release
, and/orcargo-boj
or other tools.#[path = "filepath"]
attributes for module file paths is not supported. modname.rs
, modname/mod.rs
, and inline modules are supported.trait Trait
and impl Trait for T
items, even in relatively small module structure (see ps
folder for example). This is due to the limitations of rustc
's dead code warnings and the complexity of trait resolution.cargo install --path=.
cargo oj