add array code exerciese to exerises

This commit is contained in:
leikang 2023-07-06 16:49:43 +08:00
parent a7300da78d
commit 340aa0ad5c
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,8 @@
# array
Arrays are one of the first data types beginner programmers learn. An array is a collection of elements of the same type allocated in a contiguous memory block.
## Further information
- [GitHub Repository](https://github.com/rust-lang/rust-clippy).

42
exercises/array/array1.rs Normal file
View File

@ -0,0 +1,42 @@
// array1.rs
//
// Here is the code for how the array learns
// I AM NOT DONE
use std :: vec;
fn main() {
// array definition
let number = [1,3,5,7,9,11,1,4];
let sdf = number[3];
println!("sdf is {}",sdf);
// How to create an array
let mut v = Vec::new();
v.push(1);
v.push(23);
v.push(87);
println!("v:{:?}",v);
let mut v2 = Vec::with_capacity(20);
v2.push(23);
v2.push(12);
v2.push(25);
v2.push(18);
v2.push(21);
v2.push(15);
v2.push(27);
v2.push(10);
v2.push(16);
v2.push(23);
v2.push(12);
// print result
println!("v2:{:?}",v2);
v2.pop();
println!("v2:{:?}",v2);
// v2.remove(v2[2]);
println!("v3:{:?}",v2);
}

34
exercises/array/array2.rs Executable file
View File

@ -0,0 +1,34 @@
// array2.rs
//
// Here is the code for how the array learns
// I AM NOT DONE
struct soultion;
impl soultion{
pub fn zero_move(nums: & mut Vec<i32>){
let mut i =0;
let mut j =0;
while j <nums.len(){
if nums[j] !=0{
nums[i] = nums[j];
i+=1;
}
j +=1;
}
let mut k = i;
while k < nums.len() {
nums[k] =0;
k +=1;
}
}
}
fn main() {
let mut vec:Vec<i32> = vec![0,0,3,0,5];
soultion::zero_move(& mut vec);
println!("{:?}",vec);
}