Arrays as function input

Hi, I am trying to make two function system where I take two arrays as input and give out an array as output. But, it seems like perhaps I am going wrong somewhere, which I am not able to understand correctly. Please have a look at my code.

const LEN: usize = 160;
struct MyCircuit {
    /// The input to redact we are proving that we know. Set to `None` when we
    /// are verifying a proof (and do not have the witness data).
	preimage: Option<[u8; LEN]>,
}
 
struct mask {
	mask: Option<[u8;LEN]>
}

fn main(){
 	let preimage = [42;LEN];
	let mut mask = [1;LEN];
	for i in (21..40){
			mask[i] = 0;		
		}
	for i in (43..51){
			mask[i] = 0;		
		}
	for i in (120..140){
			mask[i] = 0;		
		}
	println!("Preimage..");
	for i in 1..LEN{
		println!("{}",preimage[i]);
		}
	println!("Mask..");
	for i in 1..LEN{
		println!("{}",&mut mask[i]);
		}
	let mut masked_array = [0;LEN];
	println!("Masked Array...");
	masked_array = redact(&preimage, &mask);
	for i in 1..LEN{	
		println!("{}", &mut masked_array[i]);
	}
}

fn redact(preimage: & [u8], mask: &mut [u8] ){
	let mut internal = [0;LEN];
	for i in 1..LEN{
		internal[i] = preimage[i]*mask[i];
	}
return internal;
}

You need to specify the return type of redact.

fn redact(preimage: &[u8], mask: &mut [u8]) -> [u8; LEN] {
    ...
}
1 Like

This would have a nice solution with const_generics Rust Playground

Don't quite understand what you were trying to explain there. Sorry :frowning:

Perhaps your missing the idea that the size of the array is required in order to specify a concrete type.

In Rust you can have up to three things required to specify a concrete type:

  1. Type of the value
  2. Lifetime of the value
  3. CONST N in [T; N] to describe the size of an array (new/on its way!!)

With the latter you can create functions for any length of array... generic over array length.

Does that help?

1 Like

Sorry I was essentially nerding out.

This is, essentially, redefining const LEN: usize = x;, which is a global constant without real meaning shared everywhere, to live in the type system. This allows the compiler to check that the arrays arguments for the redact_3 function have in fact the same length before the code even runs. Which in turns allows us to return a stack allocated array instead of taking a mutable array as third argument or returning a heap allocated Vec.

Ah okay, makes sense. Thanks