These are chat archives for rust-lang/rust

18th
Apr 2017
L. Preston Sego III
@NullVoxPopuli
Apr 18 2017 00:51
Hello
I'm new here and new to rust -- I have a question on some basic syntax/typing / don't know what to do with this cow

this is my script so far:

use std::process::{Command, Output};

fn main() {
    let output = exec("ls");

    println!("stdout: {}", output);
}

fn exec(script: &str) -> String {
    let output = runCommand(script);
    let status = output.status;
    let stdout = output.stdout;

    if !status.success() {
        panic!(output.stderr)
    }

    String::from_utf8_lossy(&stdout)
}

fn runCommand(script: &str) -> Output {
    Command::new(script)
        .output()
        .expect("failed to execute process")
}

I'm getting a syntax error a the from_utf8_lossy line: expected struct std::string::String, found enum std::borrow::Cow -- I'm surprised it's an enum of std:borrow::Cow, cause in the process::Command docs, from_utf8_lossy has stdout passed to it, but that all happens in the same scope.

So, what's the proper way I should be returning the stdout output? (I'm wanting to abstract some utility functions for repeated quick shell execution and manipulating of the output (trimming, striping, subing, etc))

Corentin Henry
@little-dude
Apr 18 2017 01:02
@NullVoxPopuli use String::from_utf8_lossy(&stdout).to_string()
String::from_utf8_lossy return a Cow not a String so you need to convert it: https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8_lossy
L. Preston Sego III
@NullVoxPopuli
Apr 18 2017 01:03
oh, so String is different from the result of to_string()?
Ashley Mannix
@KodrAus
Apr 18 2017 01:03
Yeh, so the confusion is that your exec function expects to return a String, but String::from_utf8_lossy returns Cow
Corentin Henry
@little-dude
Apr 18 2017 01:04
well Cow different from String.
I agree, it's confusing that String::from_utf8_lossy does not return an actual String.
L. Preston Sego III
@NullVoxPopuli
Apr 18 2017 01:04
what's the recommended type for 'typical string manipulation' like stripping whitespace or substitution?
Ashley Mannix
@KodrAus
Apr 18 2017 01:07
Cow is a copy on write type, so if the String doesn't need to mess with the input to make it a valid UTF8 then it won't copy the input
@NullVoxPopuli Cow is a good type for operations that return a result that might be the same as the input. So in the case where you strip whitespace where there isn't any, or you do substitution when there's nothing to substitute then you can avoid unnecessarily copying things. Cow can be less friendly to work with though, so it's a balance of ergonomics and potential performance
L. Preston Sego III
@NullVoxPopuli
Apr 18 2017 01:11
hmm. ok. thanks for the explanation.
I don't think any of my use cases will have scenarios where the result will be the input (like a substitution with no matches, I suppose)
Ashley Mannix
@KodrAus
Apr 18 2017 01:14
So good old String is probably a good choice then
L. Preston Sego III
@NullVoxPopuli
Apr 18 2017 01:14
yeah. thanks for your help!, @KodrAus !
Ashley Mannix
@KodrAus
Apr 18 2017 01:15
No problem @NullVoxPopuli! :)
Michal 'vorner' Vaner
@vorner
Apr 18 2017 10:30
I guess program written in rust shouldn't segfault, should it? It is cross-compiled for armv7-unknown-linux-musleabihf, but I still think that shouldn't happen. Where should I report such thing? Rustc itself?
Michal 'vorner' Vaner
@vorner
Apr 18 2017 10:56
Hmm. It seems to work with nightly. Maybe it got fixed already.
Cajetan Puchalski
@CajetanP
Apr 18 2017 15:58
Hello !
I was wondering, is it explained anywhere how in Rust it is possible to take a refence of a number literal, like &5 ? I see that it can be done but I'm wondering how exactly does it work. I'm coming from C where it's not possible to do so and I prefer to understand what I'm actually doing :P
Diggory Blake
@Diggsey
Apr 18 2017 16:05
@CajetanP it just creates a temporary variable holding the literal value and takes the address of that
Cajetan Puchalski
@CajetanP
Apr 18 2017 16:07
Oh thanks, I guess it makes sense.. pretty interesting feature though. Very convenient in a lot of situations.
Sergey Noskov
@Albibek
Apr 18 2017 16:34
Hey guys, does Rust Evangelism Strikeforce official logo exist yet? I want one as my laptop sticker :). Just kidding of course, but does it?
Alexey
@KalitaAlexey
Apr 18 2017 16:42
Hello everyone.
If you are using vscode-rust, please review issues.
Write comments.
Open a new one if you want something else.
I am going to publish a new version in four days.
Sergey Sherkunov
@leinlawun
Apr 18 2017 20:11
Hello everyone!
Help, please, this code does not compile: https://gist.github.com/leinlawun/7f454a573f9bcff29a42330bb2e203d7
I can not understand my mistake.
Ashley Mannix
@KodrAus
Apr 18 2017 21:11
@leinlawun Looks like you might need to refactor ReverseBuffer a bit. You could either make something like an Endianness enum and use that concretely instead of the E parameter. So you'd have something like:
enum Endianness {
    Big,
    Little
}

trait ReverseBuffer {
    fn reverse_buffer(&mut self, endianness: Endianness);
}

impl<B> ReverseBuffer for B
    where B: AsMut<[u8]>
{
    fn reverse_buffer(&mut self, endianness: Endianness) {
        match endianness {
             Big => {
                  ...
             },
             Little => {
                 ...
             }
        }
    }
}
Ashley Mannix
@KodrAus
Apr 18 2017 21:19
Alternatively, you can implement traits for the BigEndian and LittleEndian structures themselves, like the byteorder crate does
Rather than for the buffer
Sergey Sherkunov
@leinlawun
Apr 18 2017 21:42
@KodrAus neither the first method nor the second one is suitable. I used to solve this problem like this: https://gist.github.com/leinlawun/3d1bf34e6040fb0fe241a7db6d9bdd65
But it did not seem elegant to me. I decided to do it in the manner described above, but I encountered a strange restriction. But thanks for the answer!