CS110L-Week 2 Exercises: Ownership and structs

第2周作业链接: https://reberhardt.com/cs110l/spring-2020/assignments/week-2-exercises/

本周将带你更加的熟悉Rust,包括所有权和引用等知识。

第一部分: 所有权


fn main()
{

    let mut s = String::from("hello");
    let ref1 = &s;
    let ref2 = &ref1;
    let ref3 = &ref2;

    //s = String::from("goodboy"); //错误 s

    println!("{}", ref3.to_uppercase());
}
fn drip_drop() -> String {

    let s = String::from("hello,world");
    
    //return &s; //错误

    return s;
}

    let s1 = String::from("hello");
    let mut v = Vec::new();
    v.push(s1);

    //let s2: String = v[0];  //错误

    let ref s2: String = v[0];
    println!("{}",s2);

第二部分:rdiff

我们将实现diff命令行实用程序的简单版本以比较两个文件。

代码:

第三部分 rwc

代码:

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

您正在使用您的 WordPress.com 账号评论。 注销 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 注销 /  更改 )

Connecting to %s