These are chat archives for rust-lang/rust

19th
Feb 2019
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;

fn main() {
  let bytes = vec![229, 133, 220, 160, 183, 63, 63, 28, 170, 72, 181, 42, 109, 239, 64, 5];
  let os_str = OsStr::from_bytes(&bytes).to_owned();
  let x = os_str.into_string();

  println!("{:?}", x);
}
Zakarum
@omni-viral
Feb 19 09:58
Why shouldn't it?
Is it valid utf-8?
Tritone
@tritone11
Feb 19 13:48
Hi, how can I split a vec in multiple vecs? Like, let's say I have [9,1,2,7,4,5,6] and I want to split it in [9,1] and [2,7,4,5,6]
Ingvar Stepanyan
@RReverser
Feb 19 13:48
.split_at()
Tritone
@tritone11
Feb 19 13:48
thanks!
Ingvar Stepanyan
@RReverser
Feb 19 13:48
although that returns slices; if you need actual vecs, then .split_off
Tritone
@tritone11
Feb 19 14:07
I noticed that it returns slices, but it's fine
thanks for the .split_off reference anyway
Chikirev Sirguy
@cheblin
Feb 19 15:47
pub fn set( d0: usize, d1: usize, SRC: Option< & Iterator<Item=i8>>) {}

fn main() {
    println!("Hello, world!");
    let src: [i8; 3] = [7, 40, 39];
    set(0, 1, Some(src.iter()));
}

error[E0308]: mismatched types
 --> src/main.rs:7:20
  |
7 |     set(0, 1, Some(src.iter()));
  |                    ^^^^^^^^^^
  |                    |
  |                    expected reference, found struct `std::slice::Iter`
  |                    help: consider borrowing here: `&src.iter()`
  |
  = note: expected type `&dyn std::iter::Iterator<Item=i8>`
             found type `std::slice::Iter<'_, i8>`

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=65ba26b5a7dbf12c8b8930f196520d16

cannot get where is the problem.

Ingvar Stepanyan
@RReverser
Feb 19 15:48
.iter() on arrays and slices returns element references, that is, &i8 in this case
but your input expects i8 value
the simplest way to get it is to use src.iter().cloned()
it's fine for primitives since they're cheap to clone
unrelated: the function shouldn't probably accept dynamic trait object
but rather be generic over any iterable
like this (example, as I don't know what it does)
pub fn set( d0: usize, d1: usize, src: Option<impl IntoIterator<Item=i8>>) {}
Chikirev Sirguy
@cheblin
Feb 19 16:05
my input expects Option< & Iterator<Item=i8>>
Ingvar Stepanyan
@RReverser
Feb 19 16:06
sorry, is this a question?
the simplest way to get it is to use src.iter().cloned()
that was already the answer to how to fix it with current types
Chikirev Sirguy
@cheblin
Feb 19 16:20
got it. thanks!
RĂ¼diger Klaehn
@rklaehn
Feb 19 20:18

What crate is most popular for basic algebraic structures (semigroups, monoids etc. )? I want to port some scala data structures to rust. https://github.com/rklaehn/abc

I found this one: https://github.com/rustsim/alga and a dozen others. Is there a more popular one?