Skip to content

Commit eb29766

Browse files
authored
Rollup merge of rust-lang#79809 - Eric-Arellano:split-once, r=matklad
Dogfood `str_split_once()` Part of rust-lang#74773. Beyond increased clarity, this fixes some instances of a common confusion with how `splitn(2)` behaves: the first element will always be `Some()`, regardless of the delimiter, and even if the value is empty. Given this code: ```rust fn main() { let val = "..."; let mut iter = val.splitn(2, '='); println!("Input: {:?}, first: {:?}, second: {:?}", val, iter.next(), iter.next()); } ``` We get: ``` Input: "no_delimiter", first: Some("no_delimiter"), second: None Input: "k=v", first: Some("k"), second: Some("v") Input: "=", first: Some(""), second: Some("") ``` Using `str_split_once()` makes more clear what happens when the delimiter is not found.
2 parents 3c58897 + 4112ff1 commit eb29766

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
#![feature(stdsimd)]
315315
#![feature(stmt_expr_attributes)]
316316
#![feature(str_internals)]
317+
#![feature(str_split_once)]
317318
#![feature(test)]
318319
#![feature(thread_local)]
319320
#![feature(thread_local_internals)]

std/src/sys_common/net.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,8 @@ impl TryFrom<&str> for LookupHost {
177177
}
178178

179179
// split the string by ':' and convert the second part to u16
180-
let mut parts_iter = s.rsplitn(2, ':');
181-
let port_str = try_opt!(parts_iter.next(), "invalid socket address");
182-
let host = try_opt!(parts_iter.next(), "invalid socket address");
180+
let (host, port_str) = try_opt!(s.rsplit_once(':'), "invalid socket address");
183181
let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
184-
185182
(host, port).try_into()
186183
}
187184
}

test/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(termination_trait_lib)]
3131
#![feature(test)]
3232
#![feature(total_cmp)]
33+
#![feature(str_split_once)]
3334

3435
// Public reexports
3536
pub use self::bench::{black_box, Bencher};

test/src/time.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,24 @@ impl TimeThreshold {
105105
/// value.
106106
pub fn from_env_var(env_var_name: &str) -> Option<Self> {
107107
let durations_str = env::var(env_var_name).ok()?;
108+
let (warn_str, critical_str) = durations_str.split_once(',').unwrap_or_else(|| {
109+
panic!(
110+
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
111+
env_var_name, durations_str
112+
)
113+
});
108114

109-
// Split string into 2 substrings by comma and try to parse numbers.
110-
let mut durations = durations_str.splitn(2, ',').map(|v| {
115+
let parse_u64 = |v| {
111116
u64::from_str(v).unwrap_or_else(|_| {
112117
panic!(
113118
"Duration value in variable {} is expected to be a number, but got {}",
114119
env_var_name, v
115120
)
116121
})
117-
});
118-
119-
// Callback to be called if the environment variable has unexpected structure.
120-
let panic_on_incorrect_value = || {
121-
panic!(
122-
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
123-
env_var_name, durations_str
124-
);
125122
};
126123

127-
let (warn, critical) = (
128-
durations.next().unwrap_or_else(panic_on_incorrect_value),
129-
durations.next().unwrap_or_else(panic_on_incorrect_value),
130-
);
131-
124+
let warn = parse_u64(warn_str);
125+
let critical = parse_u64(critical_str);
132126
if warn > critical {
133127
panic!("Test execution warn time should be less or equal to the critical time");
134128
}

0 commit comments

Comments
 (0)