feat: update tests

This commit is contained in:
2024-01-08 00:16:29 +08:00
parent 97baed2e2a
commit fe5c16450c
4 changed files with 47 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
#[derive(Debug)]
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn build_chain() -> Option<Box<Node>> {
let mut p = None;
for i in 0..3 {
p = Some(Box::new(Node {
value: i,
next: p,
}));
}
p
}
fn print_chain(mut p: &Option<Box<Node>>) {
let mut values = vec![];
while let Some(node) = p {
values.push(node.value.to_string());
p = &node.next;
}
println!("{}", values.join(" -> "))
}
fn main() {
let p = build_chain();
print_chain(&p);
}

View File

@@ -5,6 +5,6 @@ fn print_name(name: String) {
fn main() { fn main() {
let name = String::from("Hatter"); let name = String::from("Hatter");
print_name(name); print_name(name);
let name_ref = &name; // let name_ref = &name;
println!("Name: {}", name_ref); // println!("Name: {}", name_ref);
} }

View File

@@ -0,0 +1,7 @@
fn fail_and_exit() -> ! {
panic!("failed.");
}
fn main() {
fail_and_exit();
}

View File

@@ -0,0 +1,8 @@
fn len(arr: &[u8]) -> usize {
arr.len()
}
fn main() {
let a = "hello world".as_bytes();
println!("{}", len(a));
}