From 0a2ad4489a8c103b24a800892f83de1b6644b5cd Mon Sep 17 00:00:00 2001 From: lucas Date: Fri, 15 Sep 2023 02:11:46 +0800 Subject: [PATCH] feat: add Rust code in stack.md (#748) * Update stack.md Add rust sample code * Update stack.md * Update stack.md --------- Co-authored-by: Yudong Jin --- docs/chapter_stack_and_queue/stack.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index e4bd884cc..cf6656eb2 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -266,7 +266,32 @@ === "Rust" ```rust title="stack.rs" + /* 初始化栈 */ + // 把 Vec 当作栈来使用 + let mut stack: Vec = Vec::new(); + + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + /* 访问栈顶元素 */ + if let Some(top) = stack.get(stack.len() - 1) { + } + if let Some(top) = stack.last() { + } + + /* 元素出栈 */ + if let Some(pop) = stack.pop() { + } + + /* 获取栈的长度 */ + let size = stack.len(); + + /* 判断是否为空 */ + let isEmpty = stack.is_empty(); ``` === "C"