2021-10-05

Oct 6 In-Class Exercise Thread .

Please post your solutions to the Oct 6 In-Class Exercise to this thread.

Best,

Chris

Please post your solutions to the Oct 6 In-Class Exercise to this thread. Best, Chris
2021-10-06

-- Oct 6 In-Class Exercise Thread

Unmodified Program: <nowiki> fn main() { let s1 = String::from("hi there"); let s2 = s1.clone(); println!("s1 = {}, s2 = {}", s1, s2); } </nowiki>

Output: <nowiki> s1 = hi there, s2 = hi there </nowiki>

Modified Program: <nowiki> fn main() { let s1 = String::from("hi there"); let s2 = s1; println!("s1 = {}, s2 = {}", s1, s2); } </nowiki>

Output: <nowiki> error[E0382]: borrow of moved value: s1 --> main.rs:4:31 | 2 | let s1 = String::from("hi there"); | -- move occurs because s1 has type std::string::String, which does not implement the Copy trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line w... | -- value moved here 4 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try rustc --explain E0382. exit status 1 </nowiki>

Unmodified Program: <nowiki> fn main() { let s1 = String::from("hi there"); let s2 = s1.clone(); println!("s1 = {}, s2 = {}", s1, s2); } </nowiki> Output: <nowiki> s1 = hi there, s2 = hi there </nowiki> Modified Program: <nowiki> fn main() { let s1 = String::from("hi there"); let s2 = s1; println!("s1 = {}, s2 = {}", s1, s2); } </nowiki> Output: <nowiki> error[E0382]: borrow of moved value: @BT@s1@BT@ --> main.rs:4:31 | 2 | let s1 = String::from("hi there"); | -- move occurs because @BT@s1@BT@ has type @BT@std::string::String@BT@, which does not implement the @BT@Copy@BT@ trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line w... | -- value moved here 4 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move error: aborting due to previous error For more information about this error, try @BT@rustc --explain E0382@BT@. exit status 1 </nowiki>

-- Oct 6 In-Class Exercise Thread

fn main(){ let s1 = String::from(&quot;hi there&quot;); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); } fn main(){ let s1 = String::from(&quot;hi there&quot;); let s2 = s1; // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); } Resource Description for Screen Shot 2021-10-06 at 6.05.40 PM.png

fn main(){ let s1 = String::from("hi there"); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } fn main(){ let s1 = String::from("hi there"); let s2 = s1; // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } ((resource:Screen Shot 2021-10-06 at 6.05.40 PM.png|Resource Description for Screen Shot 2021-10-06 at 6.05.40 PM.png))

-- Oct 6 In-Class Exercise Thread

fn main() { let s1 = String::from(&quot;hi there&quot;); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); }

output: s1 = hi there, s2 = hi there

fn main() { let s1 = String::from(&quot;hi there&quot;); let s2 = s1; // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); }

output: error[E0382]: borrow of moved value: s1 --&gt; src/main.rs:4:30 | 2 | let s1 = String::from(&quot;hi there&quot;); | -- move occurs because s1 has type String, which does not implement the Copy trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 4 | println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); | ^^ value borrowed here after move

For more information about this error, try rustc --explain E0382. error: could not compile playground due to previous error

fn main() { let s1 = String::from("hi there"); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } output: s1 = hi there, s2 = hi there fn main() { let s1 = String::from("hi there"); let s2 = s1; // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } output: error[E0382]: borrow of moved value: @BT@s1@BT@ --> src/main.rs:4:30 | 2 | let s1 = String::from("hi there"); | -- move occurs because @BT@s1@BT@ has type @BT@String@BT@, which does not implement the @BT@Copy@BT@ trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 4 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move For more information about this error, try @BT@rustc --explain E0382@BT@. error: could not compile @BT@playground@BT@ due to previous error

-- Oct 6 In-Class Exercise Thread

&#039;&#039;&#039; Last example&#039;s output:&#039;&#039;&#039; s1 = hi there, s2 = hi there

&#039;&#039;&#039; Switch s2=s1.clone() and s2=s1 output:&#039;&#039;&#039; Compiling playground v0.0.1 (/playground) error[E0382]: borrow of moved value: s1 --&gt; src/main.rs:5:34 | 3 | let s1 = String::from(&quot;hi there&quot;); | -- move occurs because s1 has type String, which does not implement the Copy trait 4 | let s2=s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 5 | println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); | ^^ value borrowed here after move

For more information about this error, try rustc --explain E0382. error: could not compile playground due to previous error

''' Last example's output:''' s1 = hi there, s2 = hi there ''' Switch s2=s1.clone() and s2=s1 output:''' Compiling playground v0.0.1 (/playground) error[E0382]: borrow of moved value: @BT@s1@BT@ --> src/main.rs:5:34 | 3 | let s1 = String::from("hi there"); | -- move occurs because @BT@s1@BT@ has type @BT@String@BT@, which does not implement the @BT@Copy@BT@ trait 4 | let s2=s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 5 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move For more information about this error, try @BT@rustc --explain E0382@BT@. error: could not compile @BT@playground@BT@ due to previous error

-- Oct 6 In-Class Exercise Thread

&lt;nowiki&gt; Original: fn main(){ let s1 = String::from(&quot;hi there&quot;); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); }

Output: s1 = hi there, s2 = hi there

Modified:

fn main(){ let s1 = String::from(&quot;hi there&quot;); s2 = s1; println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); }

Output: error[E0425]: cannot find value s2 in this scope --&gt; test.rs:3:5 | 3 | s2 = s1; | ^^ help: a local variable with a similar name exists: s1

error[E0425]: cannot find value s2 in this scope --&gt; test.rs:4:38 | 4 | println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); | ^^ help: a local variable with a similar name exists: s1

error: aborting due to 2 previous errors

&lt;/nowiki&gt;

<nowiki> Original: fn main(){ let s1 = String::from("hi there"); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } Output: s1 = hi there, s2 = hi there Modified: fn main(){ let s1 = String::from("hi there"); s2 = s1; println!("s1 = {}, s2 = {}", s1, s2); } Output: error[E0425]: cannot find value @BT@s2@BT@ in this scope --> test.rs:3:5 | 3 | s2 = s1; | ^^ help: a local variable with a similar name exists: @BT@s1@BT@ error[E0425]: cannot find value @BT@s2@BT@ in this scope --> test.rs:4:38 | 4 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ help: a local variable with a similar name exists: @BT@s1@BT@ error: aborting due to 2 previous errors </nowiki>

-- Oct 6 In-Class Exercise Thread

fn main() { let s1 = String::from(&quot;hi there&quot;); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); }

Output: s1 = hi there, s2 = hi there

Modified:

fn main() { let s1 = String::from(&quot;hi there&quot;); let s2 = s1; println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); }

Output: error[E0382]: borrow of moved value: s1 --&gt; src/main.rs:4:34 | 2 | let s1 = String::from(&quot;hi there&quot;); | -- move occurs because s1 has type String, which does not implement the Copy trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 4 | println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); | ^^ value borrowed here after move

For more information about this error, try rustc --explain E0382.

(Edited: 2021-10-06)
fn main() { let s1 = String::from("hi there"); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } Output: s1 = hi there, s2 = hi there Modified: fn main() { let s1 = String::from("hi there"); let s2 = s1; println!("s1 = {}, s2 = {}", s1, s2); } Output: error[E0382]: borrow of moved value: @BT@s1@BT@ --> src/main.rs:4:34 | 2 | let s1 = String::from("hi there"); | -- move occurs because @BT@s1@BT@ has type @BT@String@BT@, which does not implement the @BT@Copy@BT@ trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 4 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move For more information about this error, try @BT@rustc --explain E0382@BT@.

-- Oct 6 In-Class Exercise Thread

thanhle@Thanhs-MacBook-Pro hw2_thanh % rustc main.rs thanhle@Thanhs-MacBook-Pro hw2_thanh % ./main s1 = hi there s2 = hi there

thanhle@Thanhs-MacBook-Pro hw2_thanh % rustc main2.rs error[E0382]: borrow of moved value: s1 --&gt; main2.rs:5:31 | 2 | let s1 = String::from(&quot;Hello&quot;); | -- move occurs because s1 has type String, which does not implement the Copy trait 3 | let s2 = s1; | -- value moved here 4 | 5 | println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); | ^^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try rustc --explain E0382. thanhle@Thanhs-MacBook-Pro hw2_thanh %

fn main() { let s1 = String::from(&quot;hi there\n&quot;); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work

println!("s1 = {} s2 = {}", s1, s2);

} fn main() { let s1 = String::from(&quot;Hello&quot;); let s2 = s1;

println!("s1 = {}, s2 = {}", s1, s2);

}

(Edited: 2021-10-06)
thanhle@Thanhs-MacBook-Pro hw2_thanh % rustc main.rs thanhle@Thanhs-MacBook-Pro hw2_thanh % ./main s1 = hi there s2 = hi there thanhle@Thanhs-MacBook-Pro hw2_thanh % rustc main2.rs error[E0382]: borrow of moved value: @BT@s1@BT@ --> main2.rs:5:31 | 2 | let s1 = String::from("Hello"); | -- move occurs because @BT@s1@BT@ has type @BT@String@BT@, which does not implement the @BT@Copy@BT@ trait 3 | let s2 = s1; | -- value moved here 4 | 5 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move error: aborting due to previous error For more information about this error, try @BT@rustc --explain E0382@BT@. thanhle@Thanhs-MacBook-Pro hw2_thanh % fn main() { let s1 = String::from("hi there\n"); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!("s1 = {} s2 = {}", s1, s2); } fn main() { let s1 = String::from("Hello"); let s2 = s1; println!("s1 = {}, s2 = {}", s1, s2); }

-- Oct 6 In-Class Exercise Thread

fn main() { let x = 20; let y = x;

let s1 = String::from("hi there");
// let s2= s1;

let s2 = s1.clone();
println!("s1 = {}, s2={}",s1,s2);

}

Output: s1 = hi there, s2=hi there

fn main() { let x = 20; let y = x;

let s1 = String::from("hi there");
let s2= s1;

// let s2 = s1.clone();
println!("s1 = {}, s2={}",s1,s2);

}

Output: error[E0382]: borrow of moved value: s1

fn main() { let x = 20; let y = x; let s1 = String::from("hi there"); // let s2= s1; let s2 = s1.clone(); println!("s1 = {}, s2={}",s1,s2); } Output: s1 = hi there, s2=hi there fn main() { let x = 20; let y = x; let s1 = String::from("hi there"); let s2= s1; // let s2 = s1.clone(); println!("s1 = {}, s2={}",s1,s2); } Output: error[E0382]: borrow of moved value: @BT@s1@BT@

-- Oct 6 In-Class Exercise Thread

&lt;pre&gt; Code: fn main(){ let s1 = String::from(&quot;hi there&quot;); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); } Output:s1 = hi there, s2 = hi there

Modified code error[E0382]: borrow of moved value: s1 --&gt; inclass.rs:4:31 | 2 | let s1 = String::from(&quot;hi there&quot;); | -- move occurs because s1 has type String, which does not implem ent the Copy trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 4 | println!(&quot;s1 = {}, s2 = {}&quot;, s1, s2); | ^^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try rustc --explain E0382. &lt;/pre&gt;

(Edited: 2021-10-06)
<pre> Code: fn main(){ let s1 = String::from("hi there"); let s2 = s1.clone(); // if had let s2 = s1; instead, next line would not work println!("s1 = {}, s2 = {}", s1, s2); } Output:s1 = hi there, s2 = hi there Modified code error[E0382]: borrow of moved value: @BT@s1@BT@ --> inclass.rs:4:31 | 2 | let s1 = String::from("hi there"); | -- move occurs because @BT@s1@BT@ has type @BT@String@BT@, which does not implem ent the @BT@Copy@BT@ trait 3 | let s2 = s1; // if had let s2 = s1; instead, next line would not work | -- value moved here 4 | println!("s1 = {}, s2 = {}", s1, s2); | ^^ value borrowed here after move error: aborting due to previous error For more information about this error, try @BT@rustc --explain E0382@BT@. </pre>
[ Next ]
X