## Documentation
docs: $(DOCFILES)
-.tmp/docs/%.rs: src/%.rs Makefile
+.tmp/docs/%.rs: src/%.rs Makefile dup-unimpl.sed
@mkdir -p .tmp/docs
@echo "$< -> $@"
- @# sed-fu: remove the "@" from "//@", and remove trailing "/*@*/".
- @sed 's|^\(\s*//\)@|\1|;s|\s*/\*@\*/$$||' $< > $@
+ @# sed-fu: remove the "@" from "//@", and remove trailing "/*@*/", replace lines ending in "/*@@*/" by "unimplemented!()".
+ @# Also coalesce multiple adjacent such lines to one.
+ @sed 's|^\(\s*//\)@|\1|;s|\s*/\*@\*/$$||;s|\(\s*\)\S.*/\*@@\*/|\1unimplemented!()|' $< | sed -f dup-unimpl.sed > $@
docs/%.html: .tmp/docs/%.rs
@./pycco-rs $<
// function.
fn main() {
- part10::main();
+ part00::main();
}
// Additional material
if sum >= a {
// The addition did not overflow. <br/>
// **Exercise 08.1**: Write the code to handle adding the carry in this case.
- unimplemented!()
+ let sum_total = u64::wrapping_add(sum, if carry { 1 } else { 0 }); /*@@*/
+ let had_overflow = sum_total < sum; /*@@*/
+ (sum_total, had_overflow) /*@@*/
} else {
// Otherwise, the addition *did* overflow. It is impossible for the addition of the carry
// to overflow again, as we are just adding 0 or 1.
- (sum + if carry { 1 } else { 0 }, true) /*@*/
+ (sum + if carry { 1 } else { 0 }, true) /*@*/
}
}
carry = new_carry; /*@*/
}
// **Exercise 08.2**: Handle the final `carry`, and return the sum.
- unimplemented!()
+ if carry { /*@@*/
+ result_vec.push(1); /*@@*/
+ } /*@@*/
+ BigInt { data: result_vec } /*@@*/
}
}
}
}
-fn read_one_line() -> String {
- println!("Please enter a line of text.");
- let mut stdin = io::stdin();
- let mut prefix = "".to_string();
- stdin.read_line(&mut prefix).unwrap();
- prefix
+fn print_with_prefix_v1(b: &BigInt, prefix: String) {
+ let my_action = PrintWithString { prefix: prefix };
+ b.act_v1(my_action);
}
-pub fn main_v1() {
- let prefix = read_one_line();
- let my_action = PrintWithString { prefix: prefix };
+pub fn main() {
let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
- bignum.act_v1(my_action);
+ print_with_prefix_v1(&bignum, "Digit: ".to_string());
}
impl BigInt {
}
}
-pub fn main() {
- let prefix = read_one_line();
- let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
- bignum.act(|digit| println!("{}{}", prefix, digit) );
+pub fn print_with_prefix(b: &BigInt, prefix: String) {
+ b.act(|digit| println!("{}{}", prefix, digit) );
}
//@ [index](main.html) | [previous](part08.html) | [next](main.html)
if sum >= a {
// The addition did not overflow. <br/>
// **Exercise 08.1**: Write the code to handle adding the carry in this case.
- unimplemented!()
+ let sum_total = u64::wrapping_add(sum, if carry { 1 } else { 0 }); /*@@*/
+ let had_overflow = sum_total < sum; /*@@*/
+ (sum_total, had_overflow) /*@@*/
} else {
// Otherwise, the addition *did* overflow. It is impossible for the addition of the carry
// to overflow again, as we are just adding 0 or 1.
unimplemented!()
}
// **Exercise 08.2**: Handle the final `carry`, and return the sum.
- unimplemented!()
+ if carry { /*@@*/
+ result_vec.push(1); /*@@*/
+ } /*@@*/
+ BigInt { data: result_vec } /*@@*/
}
}
}
}
-fn read_one_line() -> String {
- println!("Please enter a line of text.");
- let mut stdin = io::stdin();
- let mut prefix = "".to_string();
- stdin.read_line(&mut prefix).unwrap();
- prefix
+fn print_with_prefix_v1(b: &BigInt, prefix: String) {
+ let my_action = PrintWithString { prefix: prefix };
+ b.act_v1(my_action);
}
-pub fn main_v1() {
- let prefix = read_one_line();
- let my_action = PrintWithString { prefix: prefix };
+pub fn main() {
let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
- bignum.act_v1(my_action);
+ print_with_prefix_v1(&bignum, "Digit: ".to_string());
}
impl BigInt {
}
}
-pub fn main() {
- let prefix = read_one_line();
- let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
- bignum.act(|digit| println!("{}{}", prefix, digit) );
+pub fn print_with_prefix(b: &BigInt, prefix: String) {
+ b.act(|digit| println!("{}{}", prefix, digit) );
}