Crate page, what to look for
Start with the overview to understand the crate’s purpose.
Explore modules and key types to see what functionalities are provided.
Look at traits and their implementations to understand the crate’s design.
Examine important methods for practical usage.
Find examples to see how the crate is used in practice.
Read detailed documentation for in-depth understanding of specific functionalities.
Different use
Statements
This record comes from that I notice in one file the crates are specified differently

use crate::
use crate::{Fr, G1};
- This means that
Fr
andG1
are defined within the current crate, not in an external crate.
use ::
use ark_ec::VariableBaseMSM;
use ark_ff::{PrimeField, UniformRand};
use ark_poly::{univariate::DensePolynomial, Polynomial};
use ark_std::rand::CryptoRng;
- These lines bring items into scope from external crates.
ark_ec
,ark_ff
,ark_poly
, andark_std
are external crates specified in the[dependencies]
section of yourCargo.toml
file.
then in the lib.rs file, I found Fr and G1 as

explicit type parameter specification
Function Definition with Generics
The function generate_blinding_scalars
is defined with a generic parameter R
:
<span class="line"><span style="color: #616E88">/// generate random scalars, for blind randomness</span></span>
<span class="line"><span style="color: #D8DEE9">pub</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">fn</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">generate_blinding_scalars</span><span style="color: #ECEFF4"><</span><span style="color: #D8DEE9FF">R</span><span style="color: #81A1C1">:</span><span style="color: #D8DEE9FF"> Rng + CryptoRng</span><span style="color: #ECEFF4">>(</span><span style="color: #D8DEE9">k</span><span style="color: #81A1C1">:</span><span style="color: #D8DEE9FF"> usize</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">rng</span><span style="color: #81A1C1">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&</span><span style="color: #D8DEE9FF">mut R</span><span style="color: #ECEFF4">)</span><span style="color: #D8DEE9FF"> -> Vec</span><span style="color: #ECEFF4"><</span><span style="color: #D8DEE9FF">Fr</span><span style="color: #ECEFF4">></span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF"> (</span><span style="color: #B48EAD">0.</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">k</span><span style="color: #D8DEE9FF">)</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">map</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9">_</span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Fr</span><span style="color: #D8DEE9FF">::</span><span style="color: #88C0D0">rand</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">rng</span><span style="color: #D8DEE9FF">))</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">collect</span><span style="color: #D8DEE9FF">()</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
R
: This is a generic type parameter constrained by the traits Rng
and CryptoRng
. This means that any type R
used with this function must implement both the Rng
and CryptoRng
traits.
Function Call with Explicit Type Parameters
When calling a generic function, Rust often infers the type parameters automatically based on the arguments provided. However, there are cases where you might need to specify the type explicitly. This is done using the ::<Type>
syntax:
<span class="line"><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">b</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">generate_blinding_scalars</span><span style="color: #D8DEE9FF">::</span><span style="color: #81A1C1"><</span><span style="color: #8FBCBB">R</span><span style="color: #81A1C1">></span><span style="color: #D8DEE9FF">(40, rng);</span></span>
<span class="line"></span>
Closures
In Rust, closures are anonymous functions that you can save in a variable or pass as arguments to other functions. They are similar to lambdas in other programming languages. Closures can capture variables from the scope in which they are defined. Here’s a detailed explanation of the closure syntax in Rust:
Basic Syntax
The basic syntax for defining a closure is as follows:
<span class="line"><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">closure_name</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9">parameter1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">parameter2</span><span style="color: #D8DEE9FF">| -> ReturnType {</span></span>
<span class="line"><span style="color: #ECEFF4"> </span><span style="color: #616E88">// closure body</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
Here’s a breakdown of the components:
closure_name
: The variable that will hold the closure.|parameter1, parameter2|
: A pipe-separated list of parameters.-> ReturnType
: The optional return type of the closure.{ // closure body }
: The body of the closure.
example
<span class="line"><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">add</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9">x</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">y</span><span style="color: #D8DEE9FF">| x + y</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">add</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">2</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">3</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #88C0D0">println</span><span style="color: #81A1C1">!</span><span style="color: #D8DEE9FF">(</span><span style="color: #ECEFF4">"</span><span style="color: #A3BE8C">Result: {}</span><span style="color: #ECEFF4">"</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">result</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #616E88">// Output: Result: 5</span></span>
<span class="line"></span>
so closure are like functions, they can have name, inputs, return
enumerate()
The enumerate()
method is an iterator adaptor that transforms an iterator into a new iterator that yields pairs. Each pair consists of the index of the element and a reference to the element itself. The index starts from zero and increments by one for each subsequent element.
it adds an index
zip()
The zip()
method in Rust is a powerful iterator adaptor that combines two iterators into a single iterator of pairs (tuples). Each pair consists of one element from each of the original iterators.
code example
<span class="line"><span style="color: #D8DEE9">fn</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">main</span><span style="color: #D8DEE9FF">() </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">let</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">mut</span><span style="color: #D8DEE9FF"> vec </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">vec</span><span style="color: #81A1C1">!</span><span style="color: #D8DEE9FF">[(</span><span style="color: #B48EAD">1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">3</span><span style="color: #D8DEE9FF">)</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> (</span><span style="color: #B48EAD">4</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">2</span><span style="color: #D8DEE9FF">)</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> (</span><span style="color: #B48EAD">3</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">5</span><span style="color: #D8DEE9FF">)</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> (</span><span style="color: #B48EAD">2</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">4</span><span style="color: #D8DEE9FF">)]</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #ECEFF4"> </span><span style="color: #616E88">// Sort the vector by the second element of each tuple</span></span>
<span class="line"><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">vec</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">sort_by</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9">a</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">b</span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">a</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9FF">1</span><span style="color: #ECEFF4">.</span><span style="color: #88C0D0">cmp</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">&</span><span style="color: #D8DEE9">b</span><span style="color: #ECEFF4">.</span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">println</span><span style="color: #81A1C1">!</span><span style="color: #D8DEE9FF">(</span><span style="color: #ECEFF4">"</span><span style="color: #A3BE8C">{:?}</span><span style="color: #ECEFF4">"</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">vec</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
The .position()
Method
The .position()
method in Rust is used to find the index of the first element in an iterator that satisfies a given predicate.
<span class="line"><span style="color: #D8DEE9">fn</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">position</span><span style="color: #ECEFF4"><</span><span style="color: #D8DEE9FF">P</span><span style="color: #ECEFF4">></span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">&</span><span style="color: #D8DEE9">mut</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">self</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">predicate</span><span style="color: #D8DEE9FF">: </span><span style="color: #D8DEE9">P</span><span style="color: #D8DEE9FF">) </span><span style="color: #81A1C1">-></span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Option</span><span style="color: #81A1C1"><</span><span style="color: #D8DEE9">usize</span><span style="color: #81A1C1">></span></span>
<span class="line"><span style="color: #D8DEE9">where</span></span>
<span class="line"><span style="color: #D8DEE9FF"> P</span><span style="color: #ECEFF4">:</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">FnMut</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">&</span><span style="color: #D8DEE9">Self</span><span style="color: #D8DEE9FF">::</span><span style="color: #D8DEE9">Item</span><span style="color: #D8DEE9FF">) </span><span style="color: #81A1C1">-></span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">bool</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
self
: The iterator over which position
is called.
predicate
: A closure that takes a reference to an element and returns true
if the element satisfies the condition, and false
otherwise.
Return Value
Option<usize>
: The method returns anOption
containing the index of the first element that satisfies the predicate. If no element satisfies the predicate, it returnsNone
.
Test in Rust
running cargo test
in the root directory of a workspace will also run tests in all the submodules (sub-crates) that are part of the workspace. This is because cargo test
is designed to run tests for the entire workspace by default.
example structure
my_project/
├── Cargo.toml # Root workspace file
├── pipeline/
│ ├── Cargo.toml # Sub-crate
│ └── src/
│ └── lib.rs
└── other_crate/
├── Cargo.toml # Another sub-crate
└── src/
└── lib.rs
Root Cargo.toml
Make sure your root Cargo.toml
includes the submodules in the workspace:
[workspace]<br>members = [<br> "pipeline",<br> "other_crate"<br>]<br>
Running Tests
Simply run:
cargo test<br>
This will run all tests in the root crate as well as in all member crates listed in the workspace.members
section. Each sub-crate’s tests will be executed as part of this command.