Skip to content

join_path

Joins two pieces of a URL or path with exactly one / between them.

Trailing slashes of base and leading slashes of segment are trimmed first, so "a/" + "/b", "a" + "b" and "a//" + "b" all give "a/b". Nothing is encoded or resolved: use Url::join to resolve a reference against a base URL, and percent_encode for a segment that may contain reserved characters.

use helpers4::url::join_path;

Cargo feature url (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features url

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["url"] }
pub fn join_path(base: &str, segment: &str) -> String
ParameterTypeDescription
base&strThe first part, such as "https://api.example.com/v1/".
segment&strThe part to append, such as "/users".

String — The two parts joined by a single /.

use helpers4::url::join_path;

assert_eq!(join_path("https://api.example.com/v1/", "/users"), "https://api.example.com/v1/users");
assert_eq!(join_path("a", "b"), "a/b");

src/url/join_path.rs