admin管理员组

文章数量:1122846

In the below code I've created 2 Arc's in two different ways. So, want to know whether compiler will optimize the first Arc creation?

use std::sync::Arc;

#[derive(Debug)]
struct A {
    id: u64,
    a: i32,
    b: f64,
    c: String,
}

impl A {
    fn new(id: u64) -> Self {
        Self {
            id,
            a: 34,
            b: 3.4,
            c: "default".to_string(),
        }
    }
}

fn main() {
    let a_obj = A::new(1);
    
    // a_obj will be moved while creating arc1 which will result in copy operation.
    // So, is this slow compared to the way arc2 is created?
    // Or, compiler will optimize this operation?
    let arc1 = Arc::new(a_obj);
    let arc2 = Arc::new(A::new(2));
}

本文标签: rustCompiler optimization while creating an ArcStack Overflow