PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How to prepend "./" to a path in a platform independent way?

 September 12, 2022     cross-platform, path, platform, platform-independent, rust     No comments   

Issue

I need to prepend ./ to paths in my code, which I'm currently doing like this:

let path = Path::new("foo.sol");
let path_with_dot = Path::new("./").join(path);

However, I want to maintain compatibility across multiple platforms while adding ./ in front of the path. How can I do this?


Solution

A platform dependent const for path separators is stored at std::path::MAIN_SEPARATOR. You can use this to create platform dependent paths. However, by default the Path.join method already uses this const so instead of writing:

let path = Path::new("foo.sol");
let path_with_dot = Path::new("./").join(path);

You would just write:

let path = Path::new("foo.sol");
let path_with_dot = Path::new(".").join(path);

And the result will automatically be platform dependent.



Answered By - pretzelhammer
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing