#ans #coding #entropy #arithmetic


rANS (range variant of Asymmetric Numeral Systems) encoder and decoder

6 releases

0.3.0 Jul 12, 2023
0.2.1 Jul 26, 2022
0.1.2 Jun 15, 2022
0.1.0 Mar 29, 2021

#109 in Compression

Download history 14/week @ 2023-08-13 2/week @ 2023-08-20 8/week @ 2023-08-27 4/week @ 2023-09-03 17/week @ 2023-09-10 4/week @ 2023-09-17 3/week @ 2023-09-24 8/week @ 2023-10-01 4/week @ 2023-10-08 4/week @ 2023-10-15 8/week @ 2023-10-22 12/week @ 2023-10-29 4/week @ 2023-11-05 8/week @ 2023-11-12 13/week @ 2023-11-19 30/week @ 2023-11-26

57 downloads per month

MIT license

933 lines


Build Status crates.io Documentation MIT licensed codecov

Ranged Asymmetric Numeral Systems (rANS) encoder and decoder. Under the hood, this is a high-level wrapper over ryg-rans-sys.

ANS is a family of modern entropy coding methods introduced by Jarek Duda from Jagiellonian University. It serves as an alternative to arithmetic and Huffman coding, combining the performance and compression ratio of both. Many recent compression algorithms, such as Facebook’s Zstandard, Apple’s LZFSE, or JPEG XL, use ANS under the hood.

See the ryg_rans repository for more details about the underlying implementation.


Add the following to your Cargo.toml:

rans = "0.3.0"


use rans::byte_decoder::{ByteRansDecSymbol, ByteRansDecoder};
use rans::byte_encoder::{ByteRansEncSymbol, ByteRansEncoder};
use rans::{RansDecSymbol, RansDecoder, RansEncSymbol, RansEncoder, RansEncoderMulti};

const SCALE_BITS: u32 = 2;

// Encode two symbols
let mut encoder = ByteRansEncoder::new(1024);
let symbol1 = ByteRansEncSymbol::new(0, 2, SCALE_BITS);
let symbol2 = ByteRansEncSymbol::new(2, 2, SCALE_BITS);


let mut data = encoder.data().to_owned();

// Decode the encoded data
let mut decoder = ByteRansDecoder::new(data);
let symbol1 = ByteRansDecSymbol::new(0, 2);
let symbol2 = ByteRansDecSymbol::new(2, 2);

// Please note that the data is being decoded in reverse
assert_eq!(decoder.get(SCALE_BITS), 2); // Decoder returns cumulative frequency
decoder.advance(&symbol2, SCALE_BITS);
assert_eq!(decoder.get(SCALE_BITS), 0);
decoder.advance(&symbol1, SCALE_BITS);


The project is licensed under the MIT license.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the project by you shall be licensed as MIT, without any additional terms or conditions.



We encourage contributors to use predefined pre-commit hooks --- to install them in your local repo, make sure you have pre-commit installed and run

pre-commit install


~40K SLoC