«

Rust - 引用和借用

ljierui 发布于 阅读:121 技术杂谈


1、引用和借用

fn main() {
    let s1 = String::from("Hello");
    let len = calculate_length(&s1);
    println!("The length of {} is {} ",s1,len)

    // 修改则加入mut关键字
    let mut s2 = String::from("World");
    let len2 = calculate_length2(&mut s1);
    println!("The length of {} is {} ",s2,len2)

}

fn calculate_length( s: &String) -> usize{
    s.len()
}

fn calculate_length2( s: &mut String) -> usize{
    s.len()
}

rust语言学习

推荐阅读: