These are chat archives for rust-lang/rust

13th
Nov 2016
David McGillicuddy
@djmcgill
Nov 13 2016 21:13
What does return do inside a closure?
https://gist.github.com/djmcgill/a7b6d3cb65b0548ee163b55a363d7cd4
It seems here that it's returning from just inside itself, is there a way to get it to return from the larger function it's in?
Where SubOctants is just [Box<SVO>; 8]
Sebastian Blei
@iamsebastian
Nov 13 2016 21:18
Takes new_octants sth. like a mapping method? Looks like, the variable declaration could be used as a return statement.
David McGillicuddy
@djmcgill
Nov 13 2016 21:18
    pub fn new_octants<F: Fn(u8) -> SVO>(make_octant: F) -> SVO {
        SVO::Octants((0..8).map(|i| Box::new(make_octant(i))).collect())
    }
(SubOctants is actually ArrayVec<[Box<SVO>; 8]> mostly just for the collect method)
I changed it to unwrap and am getting a bunch of other errors, so maybe Closures make this a bit too complicated
David McGillicuddy
@djmcgill
Nov 13 2016 21:41
Okay, I created this wonderful function that does what I want:
    fn new_octants_mut_err<F: FnMut(u8) -> Result<SVO>>(mut make_octant: F) -> Result<SVO> {
        let make_boxed_octant = |i| { make_octant(i).map(|octant| Box::new(octant)) }; 
        (0..8).map(make_boxed_octant)
              .collect::<Result<SubOctants>>()
              .map(|octants| SVO::Octants(octants))
    }
Jarred Nicholls
@jnicholls
Nov 13 2016 21:54
Anyone know how to only build rust for the configured --target and not also the host target as well?
I ./configure --target=mips-unknown-linux-gnu and x86_64 is still building alongside it
waste of time :D
Jonas Platte
@jplatte
Nov 13 2016 22:09
@djmcgill You can shorten |octant| Box::new(octant) to just Box::new (same thing for SVO::Octants). And also you don't need the curly braces for the make_boxed_octant lambda.
David McGillicuddy
@djmcgill
Nov 13 2016 22:15
@jplatte thanks for the tips, I've gotten it down to
    fn new_octants_mut_err<F>(mut make_octant: F) -> Result<SVO> 
            where F: FnMut(u8) -> Result<SVO> {
        (0..8).map(|i| make_octant(i).map(Box::new))
              .collect::<Result<SubOctants>>()
              .map(|octants| SVO::Octants(octants))
    }
Which at least doesn't break the parser for Gists!
...too much
Pavel Meledin
@btbvoy
Nov 13 2016 22:28

Could somebody point me to what I'm doing wrong here. There are 3 files: main.rs, common.rs, commands.rs

common.rs looks like this:

use std::fmt;
use std::str;

#[derive(PartialEq, Debug)]
pub struct ProductId<'a> (pub &'a str);

impl<'a> fmt::Display for ProductId<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", *self)
    }
}

commands.rs looks like this:

use std::fmt;
use common::ProductId;

#[derive(PartialEq, Debug)]
pub struct CreateProduct<'a> (pub ProductId<'a>);

impl<'a> fmt::Display for CreateProduct<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", *self)
    }
}

main.rs looks like this:

mod common;
mod command;

use common::ProductId;
use command::CreateProduct;

fn main()
{
    println!("{}", CreateProduct(ProductId("1")));
}

once I do cargo build & cargo run I'm getting:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
error: Process didn't exit successfully: `target/debug/prject` (signal: 6, SIGABRT: process abort signal)

Question: Could somebody point me to what tools I could use in order to understand what exactly cause it to SOF exception to be thrown? And if you managed to that quickly pinpoint the problem - point me to it :-) Thanks in advance :-)

Nikolay Volf
@NikVolf
Nov 13 2016 22:31
probably you try to format *self recursively without an end
write!(f, "{}", *self) will call you CreateProduct::fmt which will call write! one and so on
you might want to replace write!(f, "{}", *self) with write!(f, "{}", self.0) in both cases, this is probably what you want :)
Pavel Meledin
@btbvoy
Nov 13 2016 22:36
well done
that's exactly what I missed
thanks a lot. Could you please elaborate the way to figure out it ? :-) I just recently started playing with rust and didn't develop yep proper mindset for rust programming :-D so I really appreciate if you could trace how you come to such a conclusion :-)
David McGillicuddy
@djmcgill
Nov 13 2016 22:49
Is it normal to have to duplicate code like this?
    pub fn new_octants<F>(make_octant: F) -> SVO 
            where F: Fn(u8) -> SVO {
        SVO::Octants(
            (0..8).map(|i| Box::new(make_octant(i)))
                  .collect())
    }

    pub fn new_octants_mut<F>(mut make_octant: F) -> SVO
            where F: FnMut(u8) -> SVO {
        SVO::Octants(
            (0..8).map(|i| Box::new(make_octant(i)))
                  .collect::<SubOctants>()
        )
    }
(and then of course there's the new_octants_mut_err I spoke about above)
Wait, Fn extends FnMut so I can just define the second one and use it with all my Fns too.
Nikolay Volf
@NikVolf
Nov 13 2016 22:58
@btbvoy
that's easy, for stack overflow never-ending recursion is always the first suspect, so you just have to check where can it happen