parent() - > Option<Path>- 수신자
- Path
- 최소 인자 수
- 0
- 최대 인자 수
- 0
Declaration and parameters
링크사용 조건
기본 포함 · 미정
Import std.path in language mode 5.21.
The current compiler selects std/path.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
수신자
Path
매개변수 제약
A Path is immutable normalized relative text. Every member evaluates its receiver and arguments once in ordinary call order. Members perform no host access and leave the receiver unchanged on success and failure.
to returns the exact normalized representation. Separators are forward slashes and the relative root is a dot. Constructing a Path from that text returns the same representation.
join accepts nonempty relative child text. It rejects NUL, a leading separator, and a colon immediately after one ASCII character before combining the child with the receiver. Both slash forms are separators. Parent components remove preceding components from the combined path and return Err when none remain to remove.
Joining a child may reach the relative root but may not cross it. A dot child preserves the receiver. Invalid child text returns Err containing an error string without producing a Path. Error text identifies Path.join and the invalid condition.
parent removes the final component. For a one component path it returns Some containing the dot root. The relative root has no parent and returns None().
file returns the final component without any preceding separator. The relative root returns None(). These results describe text and do not establish whether the path identifies a file or directory.
extension returns the text after the final dot in the file name, provided that dot is not its first character. A leading dot alone does not introduce an extension. A final dot returns Some(""). A name without such a dot and the relative root return None().
with replaces the final extension or appends one when it is absent. The argument supplies the suffix without a leading dot. An empty argument removes an existing suffix and its separating dot. A leading dot, either separator, or NUL in the argument returns Err.
with preserves a leading dot that belongs to the file name. It returns Err when the receiver is the relative root because that path has no file name. A successful replacement is normalized with the same rules as Path.from.
Path validation describes lexical text. Host operations may reject an otherwise valid Path because of platform naming rules, permissions, or capability restrictions. A Path does not establish filesystem existence or prevent a symbolic link from resolving elsewhere.
관련 규칙 · Inspect and transform normalized paths →결과
The declared result type is Option<Path>.
효과
실패
Result and effects
링크Return the path without its final component. A one component path has the dot root as its parent, and the dot root has no parent.
Related entries
링크Inspect and transform normalized paths
링크Inspect and transform normalized paths
A Path is immutable normalized relative text. Every member evaluates its receiver and arguments once in ordinary call order. Members perform no host access and leave the receiver unchanged on success and failure.
to returns the exact normalized representation. Separators are forward slashes and the relative root is a dot. Constructing a Path from that text returns the same representation.
join accepts nonempty relative child text. It rejects NUL, a leading separator, and a colon immediately after one ASCII character before combining the child with the receiver. Both slash forms are separators. Parent components remove preceding components from the combined path and return Err when none remain to remove.
Joining a child may reach the relative root but may not cross it. A dot child preserves the receiver. Invalid child text returns Err containing an error string without producing a Path. Error text identifies Path.join and the invalid condition.
parent removes the final component. For a one component path it returns Some containing the dot root. The relative root has no parent and returns None().
file returns the final component without any preceding separator. The relative root returns None(). These results describe text and do not establish whether the path identifies a file or directory.
extension returns the text after the final dot in the file name, provided that dot is not its first character. A leading dot alone does not introduce an extension. A final dot returns Some(""). A name without such a dot and the relative root return None().
with replaces the final extension or appends one when it is absent. The argument supplies the suffix without a leading dot. An empty argument removes an existing suffix and its separating dot. A leading dot, either separator, or NUL in the argument returns Err.
with preserves a leading dot that belongs to the file name. It returns Err when the receiver is the relative root because that path has no file name. A successful replacement is normalized with the same rules as Path.from.
Path validation describes lexical text. Host operations may reject an otherwise valid Path because of platform naming rules, permissions, or capability restrictions. A Path does not establish filesystem existence or prevent a symbolic link from resolving elsewhere.
이유
One normalized representation makes composition predictable. Parent and extension queries describe components without consulting host state. A suffix has one accepted spelling, and joining validates the child before its leading separator can disappear inside the combined text.
Join relative components without changing the receiver
링크import std.path { Path }import std.testimport std.iolet base = test.assertOk(Path.from("chapters/draft"))let child = test.assertOk(base.join("../final/./main.tpz"))io.writeLine(base.toString())io.writeLine(child.toString())io.writeLine(test.assertOk(base.join("../..")).toString())io.writeLine(test.assertOk(base.join(".")).toString())
chapters/draft
chapters/final/main.tpz
.
chapters/draft
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
chapters/draftchapters/final/main.tpz.chapters/draft
Reject invalid child text before joining
링크import std.path { Path }import std.testimport std.iolet base = test.assertOk(Path.from("chapters"))for child in (["", "/outside", "C:/outside", "../../outside"]) {match (base.join(child)) {case Err(_) => io.writeLine("rejected")case Ok(_) => io.writeLine("accepted")}}io.writeLine(base.toString())
rejected
rejected
rejected
rejected
chapters
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
rejectedrejectedrejectedrejectedchapters
Inspect the final component and the relative root
링크import std.path { Path }import std.testimport std.iolet file = test.assertOk(Path.from("chapters/main.tpz"))io.writeLine(test.assertSome(file.parent()).toString())io.writeLine(test.assertSome(file.fileName()))io.writeLine(test.assertSome(file.extension()))let root = test.assertOk(Path.from("."))test.assertNone(root.parent())test.assertNone(root.fileName())test.assertNone(root.extension())io.writeLine(root.toString())
chapters
main.tpz
tpz
.
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
chaptersmain.tpztpz.
Keep the relative root as the parent of one component
링크import std.path { Path }import std.testimport std.iolet file = test.assertOk(Path.from("main.tpz"))match (file.parent()) {case Some(parent) => io.writeLine(parent.toString())case None() => io.writeLine("missing")}
.
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
.
Distinguish an absent extension from an empty extension
링크import std.path { Path }import std.testimport std.iofor name in (["README", ".env", ".env.local", "archive.tar.gz", "trailing."]) {let file = test.assertOk(Path.from(name))match (file.extension()) {case Some(extension) => io.writeLine("present [{extension}]")case None() => io.writeLine("absent")}}
absent
absent
present [local]
present [gz]
present []
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
absentabsentpresent [local]present [gz]present []
Replace and remove a final extension
링크import std.path { Path }import std.testimport std.iolet file = test.assertOk(Path.from("chapters/main.tpz"))io.writeLine(test.assertOk(file.withExtension("pdf")).toString())io.writeLine(test.assertOk(file.withExtension("")).toString())io.writeLine(file.toString())
chapters/main.pdf
chapters/main
chapters/main.tpz
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
chapters/main.pdfchapters/mainchapters/main.tpz
Preserve the name of a file that starts with a dot
링크import std.path { Path }import std.testimport std.iolet file = test.assertOk(Path.from(".env"))io.writeLine(test.assertOk(file.withExtension("local")).toString())io.writeLine(test.assertOk(file.withExtension("")).toString())
.env.local
.env
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
.env.local.env
Reject a second suffix spelling and a path without a file name
링크import std.path { Path }import std.testimport std.iolet file = test.assertOk(Path.from("chapter.txt"))for extension in ([".pdf", "pdf/extra", "pdf\\extra"]) {match (file.withExtension(extension)) {case Err(_) => io.writeLine("rejected")case Ok(_) => io.writeLine("accepted")}}let root = test.assertOk(Path.from("."))match (root.withExtension("pdf")) {case Err(_) => io.writeLine("root rejected")case Ok(_) => io.writeLine("root accepted")}
rejected
rejected
rejected
root rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
rejectedrejectedrejectedroot rejected