From 9baf4a1753ab097e574046431dfbbd488a6739b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=A5=E9=A3=8E?= <77157236+night-cruise@users.noreply.github.com> Date: Mon, 13 Nov 2023 14:18:29 +0800 Subject: [PATCH] Use Vec.last() method to access the top item of stack. (#942) * Use Vec.last() method to access the top item of stack. * Use Vec.last() method to access the top item of stack. --- codes/rust/chapter_stack_and_queue/stack.rs | 2 +- docs/chapter_stack_and_queue/stack.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/rust/chapter_stack_and_queue/stack.rs b/codes/rust/chapter_stack_and_queue/stack.rs index 4fd7c1e6b..be392a917 100644 --- a/codes/rust/chapter_stack_and_queue/stack.rs +++ b/codes/rust/chapter_stack_and_queue/stack.rs @@ -22,7 +22,7 @@ pub fn main() { print_util::print_array(&stack); // 访问栈顶元素 - let peek = stack.get(stack.len() - 1).unwrap(); + let peek = stack.last().unwrap(); print!("\n栈顶元素 peek = {peek}"); // 元素出栈 diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 2b6619a50..581459b8c 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -278,7 +278,7 @@ stack.push(4); /* 访问栈顶元素 */ - let top = stack[stack.len() - 1]; + let top = stack.last().unwrap(); /* 元素出栈 */ let pop = stack.pop().unwrap();