---
title: "Discussion on \"Rust 中的全局状态\" | Hashnode"
description: "Discussion on \"Rust 中的全局状态\". 最近写 Rust 程序需要用到全局状态，我希望有一个状态在公共的作用域里，其他模块能通过引入等方式去访问他，在 JS 中做起来很简单：\nconst globalState = {\n    name: 'abc'\n}\n\nfunction foo() {\n  console.log(globalState)\n}\n\nfunction bar() {\n  globalState.name = '123'\n}\n\n由于 JS 的限制较少，变量声明的位置没有限制，在代码顶部声明后，后续所有作用域都能使用。\n在..."
url: "https://hashnode.com/posts/rust-1/66ade78aaeb65fcb4f1426b6"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Rust 中的全局状态](https://erio.hashnode.dev/rust-1?utm_source=hashnode&utm_medium=feed)

最近写 Rust 程序需要用到全局状态，我希望有一个状态在公共的作用域里，其他模块能通过引入等方式去访问他，在 JS 中做起来很简单： const globalState = { name: 'abc' } function foo() { console.log(globalState) } function bar() { globalState.name = '123' } 由于 JS 的限制较少，变量声明的位置没有限制，在代码顶部声明后，后续所有作用域都能使用。 在...

Aug 3, 2024 · 1 min read · erio.hashnode.dev  
Read the article

-   [#rust](https://hashnode.com/tag/rust)
-   [#global-variables](https://hashnode.com/tag/global-variables)
-   [#2articles1week](https://hashnode.com/tag/2articles1week)

## Written by

[Erioifpud](https://hashnode.com/@Erioifpud)

@Erioifpud

  
[View profile](https://hashnode.com/@Erioifpud)

Development

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
