Skip to content

Commit

Permalink
Use Vec.last() method to access the top item of stack. (krahets#942)
Browse files Browse the repository at this point in the history
* Use Vec.last() method to access the top item of stack.

* Use Vec.last() method to access the top item of stack.
  • Loading branch information
night-cruise authored Nov 13, 2023
1 parent 2b0cf6f commit 9baf4a1
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion codes/rust/chapter_stack_and_queue/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");

// 元素出栈
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_stack_and_queue/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
stack.push(4);

/* 访问栈顶元素 */
let top = stack[stack.len() - 1];
let top = stack.last().unwrap();

/* 元素出栈 */
let pop = stack.pop().unwrap();
Expand Down

0 comments on commit 9baf4a1

Please sign in to comment.