• 0 Posts
  • 19 Comments
Joined 1 year ago
cake
Cake day: June 7th, 2025

help-circle




  • brandon@piefed.socialtoLinux@lemmy.mlUsing alias of path in commands?
    link
    fedilink
    English
    arrow-up
    5
    ·
    edit-2
    2 months ago

    By default bash will only expand an alias if it’s the first argument of the command (that is, the command itself).

    It’s probably not intended to use aliases this way, and there are probably better options for you.

    However, there is a little trick you can do. If the alias command ends in a space, then bash will also check the next argument:

    alias cd='cd '

    Notice the trailing space after ‘cd’ in the alias definition.

    alias docs='/media/docs'

    then, cd docs should work the way you expect.

    Another method would be to:

    alias docs='echo /media/docs'

    Then you can do

    cd `docs`  
    

    The backticks will cause the shell to replace that portion with the output of the docs shell command, which will be expanded via the alias.

    All that said, it’s probably easiest just to use a link, like another commenter suggested.













  • brandon@piefed.socialtoProgrammer Humor@lemmy.mlrustmas
    link
    fedilink
    English
    arrow-up
    6
    ·
    7 months ago

    Not really, because rust doesn’t have exceptions. Instead you are encouraged to handle every possible case with pattern matching. For example:

    fn maybe_add_one(number: Option<u8>) -> u8 {
        match number {
            None => 0,
            Some(i) => i + 1,
        }
    }
    

    Option<u8> is a type which can either be some 8bit unsigned integer, or none. It’s conceptually similar to a Nullable<int> in C#.

    In C# you could correctly implement this like:

    public int MaybeAddOne(int? number)
    {
        if (number.HasValue)
        {
            return number.Value + 1;
        }
    
        return 0;
    } 
    

    In rust, you can call Unwrap on an option to get the underlying value, but it will panic if the value is None (because None isn’t a u8):

    fn maybe_add_one(number: Option<u8>) -> u8 {
        number.unwrap() + 1
    }
    

    In some cases unwrap could be useful if you don’t care about a panic or if you know the value can’t be None. Sometimes it’s just used as a shortcut. You can likewise do this in C#:

    public int MaybeAddOne(int? number)
    {
        return number.Value + 1;
    } 
    

    But this throws an exception if number is null.

    A panic isn’t the same as an exception though, you can’t ‘catch’ a panic, it’s unrecoverable and the program will terminate more-or-less immediately.

    Rust provides a generic type Result<T, E>, T being a successful result and E being some error type, which you are encouraged to use along with pattern matching to make sure all cases are handled.



  • brandon@piefed.socialtoProgrammer Humor@lemmy.mlrustmas
    link
    fedilink
    English
    arrow-up
    1
    ·
    7 months ago

    It’s more like a method that can throw an exception. Rust doesn’t really have exceptions, but if you have a Result<T> or Option<T> type you can Unwrap it to get just the T. But if there’s no T to get (in the case of an Error type for Result for None for Option) the call panics.