Skip to content

Instantly share code, notes, and snippets.

@stegaBOB
Created November 4, 2022 04:42
Show Gist options
  • Save stegaBOB/cebca3d013758ccef69e7059467f5a6b to your computer and use it in GitHub Desktop.
Save stegaBOB/cebca3d013758ccef69e7059467f5a6b to your computer and use it in GitHub Desktop.
Using Into to implement From builds and causes a stack overflow at runtime
#[derive(Default, Debug)]
pub struct Bar {
pub bob: String,
}
#[derive(Default, Debug)]
pub struct Foo {
pub alice: u16,
}
impl From<Bar> for Foo {
fn from(bar_struct: Bar) -> Self {
Into::<Self>::into(bar_struct)
}
}
fn main() {
let new_bar = Bar::default();
println!("New Bar: {:?}", new_bar);
let new_foo: Foo = new_bar.into();
println!("New Foo: {:?}", new_foo);
}
@stegaBOB
Copy link
Author

stegaBOB commented Nov 4, 2022

Output:

   Compiling from-into-stack-overflow v0.1.0 (/from-into-stack-overflow)
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `target/debug/from-into-stack-overflow`
New Bar: Bar {
    bob: "Bob",
}

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[1]    57192 abort      cargo run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment