@@ -5,7 +5,7 @@ question: |-
55 fn main() {
66 let paths = vec![
77 "/usr/bin/../lib/file.so",
8- "relative/../ path/to /file",
8+ "relative/path/.. /file",
99 "../../../etc/passwd",
1010 "normal/path/file.txt"
1111 ];
@@ -23,14 +23,14 @@ question: |-
2323
2424 What does this code print?
2525answers :
26- - All print `Some("file")` as the parent directory name
27- - First prints `Some("lib")`, second prints `Some("to") `, third prints `Some("etc")`, fourth prints `Some("path")`
28- - First prints `Some("lib")`, second prints `Some("to ")`, third prints `None `, fourth prints `Some("path")`
26+ - ' `Some("file.so")`, `Some("file")`, `Some("passwd")`, `Some("file.txt")` '
27+ - ' `Some("lib")`, `None `, `Some("etc")`, `Some("path")`'
28+ - ' `Some("lib")`, `Some(".. ")`, `Some("etc") `, `Some("path")`'
2929- All print `None` because parent directories cannot be determined from strings
3030correct_answer : 1
3131expected_output :
3232- ' /usr/bin/../lib/file.so: Some("lib")'
33- - ' relative/../path/to/ file: Some("to") '
33+ - ' relative/path/ ../file: None '
3434- ' ../../../etc/passwd: Some("etc")'
3535- ' normal/path/file.txt: Some("path")'
3636explanation : |-
@@ -44,21 +44,16 @@ explanation: |-
4444 For `/usr/bin/../lib/file.so`, the parent is `/usr/bin/../lib`, and its file
4545 name component is `lib`, resulting in `Some("lib")`.
4646
47- For `relative/../path/to/ file`, the parent is `relative/../ path/to`, and its
48- file name component is `to`, resulting in `Some("to") `.
47+ For `relative/path/ ../file`, the parent is `relative/path/..`. The final
48+ component `..` is not a valid file name, so the result is `None `.
4949
50- For `../../../etc/passwd`, the parent is `../../../etc`. Rust's `Path` API
51- performs lexical (string-based) path operations. When extracting the file name
52- from `../../../etc`, it treats `etc` as a regular path component and returns
53- `Some("etc")`. The `..` components earlier in the path don't affect the file
54- name extraction of the final component.
50+ For `../../../etc/passwd`, the parent is `../../../etc`, and its file name
51+ component is `etc`, resulting in `Some("etc")`.
5552
5653 For `normal/path/file.txt`, the parent is `normal/path`, and its file name
5754 component is `path`, resulting in `Some("path")`.
5855
5956 The key takeaway is that Rust's `Path` API performs lexical operations without
6057 filesystem access or path normalization. The `file_name()` method extracts the
61- final component of a path as a string operation, treating each segment between
62- separators as a distinct component. Path components like `..` are treated as
63- regular components in the path string, and `file_name()` simply returns the last
64- non-empty component.
58+ final component of a path, but returns `None` for special components like `..`
59+ and `.`, as these are not considered valid file names.
0 commit comments