rust-by-practice/solutions/collections/Vector.md

271 lines
4.5 KiB
Markdown
Raw Normal View History

2022-05-01 00:08:50 -05:00
1.
2022-03-08 00:23:15 -06:00
```rust
fn main() {
let arr: [u8; 3] = [1, 2, 3];
let v = Vec::from(arr);
is_vec(&v);
let v = vec![1, 2, 3];
is_vec(&v);
// vec!(..) and vec![..] are same macros, so
let v = vec!(1, 2, 3);
is_vec(&v);
// in code below, v is Vec<[u8; 3]> , not Vec<u8>
// USE `for` to rewrite the below code
let mut v1 = Vec::new();
for i in &v {
v1.push(*i)
}
is_vec(&v1);
assert_eq!(format!("{:?}",v), format!("{:?}",v1));
println!("Success!")
}
fn is_vec(v: &Vec<u8>) {}
//Another solution
fn main() {
let arr: [u8; 3] = [1, 2, 3];
let v = Vec::from(arr);
is_vec(&v);
let v = vec![1, 2, 3];
is_vec(&v);
// vec!(..) and vec![..] are same macros, so
let v = vec!(1, 2, 3);
is_vec(&v);
// in code below, v is Vec<[u8; 3]> , not Vec<u8>
// USE Vec::new and `for` to rewrite the below code
let mut v1 = vec!();
for i in &v{
v1.push(*i);
}
is_vec(&v1);
assert_eq!(v, v1);
println!("Success!")
}
fn is_vec(v: &Vec<u8>) {}
2022-03-08 00:23:15 -06:00
```
2.
2022-05-01 00:08:50 -05:00
2022-03-08 00:23:15 -06:00
```rust
fn main() {
let mut v1 = Vec::from([1, 2, 4]);
v1.pop();
v1.push(3);
let mut v2 = Vec::new();
v2.extend([1, 2, 3]);
assert_eq!(format!("{:?}",v1), format!("{:?}",v2));
println!("Success!")
}
```
2022-05-01 00:08:50 -05:00
3.
2022-03-08 00:23:15 -06:00
```rust
fn main() {
// array -> Vec
let arr = [1, 2, 3];
let v1 = Vec::from(arr);
let v2: Vec<i32> = arr.into();
assert_eq!(v1, v2);
// String -> Vec
let s = "hello".to_string();
let v1: Vec<u8> = s.into();
let s = "hello".to_string();
let v2 = s.into_bytes();
assert_eq!(v1, v2);
let s = "hello";
let v3 = Vec::from(s);
assert_eq!(v2, v3);
println!("Success!")
}
2022-10-10 21:44:22 -05:00
```
2022-03-08 00:23:15 -06:00
2022-05-01 00:08:50 -05:00
4.
2022-03-08 00:23:15 -06:00
```rust,editable
fn main() {
let mut v = Vec::from([1, 2, 3]);
for i in 0..5 {
println!("{:?}", v.get(i))
}
for i in 0..5 {
if let Some(x) = v.get(i) {
v[i] = x + 1
} else {
v.push(i + 2)
}
}
assert_eq!(format!("{:?}",v), format!("{:?}", vec![2, 3, 4, 5, 6]));
println!("Success!")
}
//Another solution
fn main() {
let mut v = Vec::from([1, 2, 3,4,5]);
for i in 0..5 {
println!("{:?}", v[i])
}
for i in 0..5 {
v[i] +=1;
}
assert_eq!(v, vec![2, 3, 4, 5, 6]);
println!("Success!")
}
2022-03-08 00:23:15 -06:00
```
2022-05-01 00:08:50 -05:00
5.
2022-03-08 00:23:15 -06:00
```rust
// FIX the errors
fn main() {
let mut v = vec![1, 2, 3];
let slice1 = &v[..];
// out of bounds will cause a panic
// You must use `v.len` here
let slice2 = &v[0..v.len()];
assert_eq!(slice1, slice2);
// slice are read only
// Note: slice and &Vec are different
let vec_ref: &mut Vec<i32> = &mut v;
(*vec_ref).push(4);
let slice3 = &mut v[0..];
assert_eq!(slice3, &[1, 2, 3, 4]);
println!("Success!")
}
```
2022-05-01 00:08:50 -05:00
6.
2022-03-08 00:23:15 -06:00
```rust
// FIX the errors
fn main() {
let mut vec = Vec::with_capacity(10);
// The vector contains no items, even though it has capacity for more
assert_eq!(vec.len(), 0);
assert_eq!(vec.capacity(), 10);
// These are all done without reallocating...
for i in 0..10 {
vec.push(i);
}
assert_eq!(vec.len(), 10);
assert_eq!(vec.capacity(), 10);
// ...but this may make the vector reallocate
vec.push(11);
assert_eq!(vec.len(), 11);
assert!(vec.capacity() >= 11);
// fill in an appropriate value to make the `for` done without reallocating
let mut vec = Vec::with_capacity(100);
for i in 0..100 {
vec.push(i);
}
assert_eq!(vec.len(), 100);
assert_eq!(vec.capacity(), 100);
println!("Success!")
}
```
2022-05-01 00:08:50 -05:00
7.
2022-03-08 00:23:15 -06:00
```rust
#[derive(Debug, PartialEq)]
enum IpAddr {
V4(String),
V6(String),
}
2022-05-01 00:08:50 -05:00
2022-03-08 00:23:15 -06:00
fn main() {
// FILL in the blank
2022-05-01 00:08:50 -05:00
let v: Vec<IpAddr> = vec![
2022-03-08 00:23:15 -06:00
IpAddr::V4("127.0.0.1".to_string()),
IpAddr::V6("::1".to_string())
];
2022-05-01 00:08:50 -05:00
2022-03-08 00:23:15 -06:00
// Comparing two enums need to derive the PartialEq trait
assert_eq!(v[0], IpAddr::V4("127.0.0.1".to_string()));
assert_eq!(v[1], IpAddr::V6("::1".to_string()));
println!("Success!")
}
```
2022-05-01 00:08:50 -05:00
8.
2022-03-08 00:23:15 -06:00
```rust
trait IpAddr {
fn display(&self);
}
struct V4(String);
impl IpAddr for V4 {
fn display(&self) {
println!("ipv4: {:?}",self.0)
}
}
struct V6(String);
impl IpAddr for V6 {
fn display(&self) {
println!("ipv6: {:?}",self.0)
}
}
fn main() {
let v: Vec<Box<dyn IpAddr>> = vec![
Box::new(V4("127.0.0.1".to_string())),
Box::new(V6("::1".to_string())),
];
for ip in v {
ip.display();
}
}
```