pub struct Env { /* private fields */ }Expand description
环境变量管理器 — 对齐 PHP think\facade\Env
通过 .env 文件加载配置,同时支持读取真实进程环境变量。
§优先级
get() 查找顺序:
- 真实进程环境变量
std::env::var(name) - 内部存储(
.env文件加载或set()写入的值)
§线程安全
内部存储通过 Arc<RwLock<HashMap>> 保护,支持并发读、互斥写。
不调用 std::env::set_var,避免 Rust 2024 edition 的线程安全警告。
§PHP 对齐
// PHP think\facade\Env
Env::load('.env'); // 加载 .env 文件
Env::set('APP_KEY', 'xxx'); // 设置内部变量
Env::get('APP_KEY'); // 获取(优先真实环境变量)
Env::has('APP_KEY'); // 检查存在Implementations§
Source§impl Env
impl Env
Sourcepub async fn load_from_file(
&self,
path: impl AsRef<Path>,
) -> Result<(), EnvError>
pub async fn load_from_file( &self, path: impl AsRef<Path>, ) -> Result<(), EnvError>
从 .env 文件加载配置
支持 INI 风格的 [section] 段,section 内的键会以 section.key 形式存储。
§参数
path:.env文件路径
§返回
成功返回 Ok(()),失败返回 EnvError。
§PHP 对齐
Env::load('.env');§错误
EnvError::FileRead: 文件读取失败EnvError::Parse: 文件解析失败(格式错误)
Sourcepub fn get_with_default(&self, name: &str, default: &str) -> String
pub fn get_with_default(&self, name: &str, default: &str) -> String
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Env
impl !UnwindSafe for Env
impl Freeze for Env
impl Send for Env
impl Sync for Env
impl Unpin for Env
impl UnsafeUnpin for Env
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.