The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Get A Filename From A Rust Path Without The Extension

TL;DR

Use `file_stem()`` to get a filename from a path without its extensio

TODO: Pull subtitle into page object
Code
use std::path::PathBuf;

fn main() {
  let path = PathBuf::from("some/path/to/alfa.html");
  let file_name = path.file_stem().unwrap().to_string_lossy();
  println!("{}", file_name);
}
Results
alfa

Notes

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Get A Filename From A Rust Path Without The Extension

-- tldr/

Use `file_stem()`` to get a filename from a path
without its extensio

-- /tldr



-- code
-- rust

use std::path::PathBuf;

fn main() {
  let path = PathBuf::from("some/path/to/alfa.html");
  let file_name = path.file_stem().unwrap().to_string_lossy();
  println!("{}", file_name);
}

-- results/

alfa

-- /results



-- notes

- `file_stem()`` gets the filename up until the last
dot. That means if the input is something like `alfa.tar.gz``
the output will be `alfa.tar`` instead of `alfa``

- There's a `file_prefix()`` in nightly Rust that gets 
the name up until the first dot. (i.e. given `alfa.tar.gz``
it will return `alfa``)

- `file_stem()`` returns an `OsString``. I'm converting
it to a regular `String`` here with `to_string_lossy()``.
It makes sure that invalid UTF characters are replaced
with a placeholder instead of breaking the string


-- ref
-- url: https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.file_stem
-- title: file_stem()


-- ref
-- url: https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.file_prefix
-- title: file_prefix()

-- ref
-- url: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
-- title: OsStr

-- ref
-- url: https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.to_string_lossy
-- title: OsStr to_string_lossy()



-- categories
-- Rust 

-- metadata
-- date: 2024-01-03 11:12:46
-- id: 2as3tnh7
-- site: aws
-- type: post
-- status: published