Merge conflict in Rust
In this example git will merge the changes automatically, but the the merged version will not compile.
In one branch:
- Run the code:
cargo run 17This works. - Change the iterations from 45 to 100 and run
cargo run 17again. This will have apanic!“attempt to add with overflow” - Change the number type from
u32tou128. Now it compiles and runs.
In another branch:
- Enable both the definition and the usage of the
squarefunction. - Run
cargo run 17. It works.
Merge them together. It has a compilation error.
fn main() {
let args = std::env::args().collect::<Vec<String>>();
if args.len() != 2 {
println!("Usage: {} NUMBER", &args[0]);
std::process::exit(1);
}
let n = args[1].parse::<u32>().unwrap();
println!("fibo({n}) = {}", fibo(n));
//println!("square({n}) = {}", square(n));
println!("----------");
for k in 1..45 {
let fib = fibo(k);
if fib % n == 0 {
println!("fibo({k}) = {fib} can be divided by {n}");
}
}
}
fn fibo(n: u32) -> u32 {
if n == 1 {
return 0;
}
if n == 2 {
return 1;
}
let mut fibs = vec![0, 1];
for _ in 2..n {
fibs.push(fibs[fibs.len() - 1] + fibs[fibs.len() - 2])
}
fibs[fibs.len() - 1]
}
// fn square(n: u32) -> u32 {
// n*n
// }