| Crates.io | nut_sys |
| lib.rs | nut_sys |
| version | 0.1.2 |
| created_at | 2024-12-15 20:36:02.616777+00 |
| updated_at | 2025-02-24 18:32:14.921129+00 |
| description | Wrapper for Number-Theory-Utils C library |
| homepage | |
| repository | https://github.com/hacatu/nut_sys |
| max_upload_size | |
| id | 1484399 |
| size | 36,557 |
nut_sysnut_sys is a low-level Rust wrapper for the Number Theory Utils (nut) C library. It provides raw FFI bindings and some safe wrappers. Not all functions from the C library are currently exported, more will be added as needed.
nut library.nut C library from a submodule (requires --features build-c).nut LibraryIf you already have the nut library installed on your system, you might need to set the NUT_LIB_DIR environment variable if it is in a non-standard location.
Then just run cargo build as normal:
export NUT_LIB_DIR=/path/to/nut/lib
cargo build
For example, the default install location is /usr/local/lib/, which is usually in the library path, but not for some linkers like mold.
nut from SourceIf you do not have nut installed and do not want to manually build it:
Clone the repository and initialize the submodule:
git submodule update --recursive --init
Build the Rust crate with the build-c feature enabled:
cargo build --features build-c
This will compile the nut library and link it statically to your Rust crate.
You can also then install or run more tests on the copy of nut that cargo downloaded, but be careful because
changing the <repository>/nut/ directory can cause build.rs to re-run.
Add nut_sys to your Cargo.toml:
[dependencies]
nut_sys = "0.1.0" # Replace with the latest version from crates.io
Then, in your Rust code:
use nut_sys::{sieve_largest_factors, Factors};
fn main() -> Result<(), ThinBox<dyn Error>> {
let largest_factors = sieve_largest_factors(100_0000u64);
let largest_factors: &[_] = largest_factors.borrow();
let mut count = 0;
let mut fxn = Factors::make_ub(100_000);
for n in 1..=100_000 {
fxn.fill_from_largest_factors(n, largest_factors);
if (0..fxn.num_primes()).all(|i|fxn[i].power == 1) {
count += 1;
}
}
println!("Found {count} squarefree numbers up to 100_000");
Ok(())
}
nut_sys and nut are both licensed under MPL-2.0. See LICENSE for details.